我有一个MySQL数据库,其中有一个名为'posts'的表我正在通过FeathersJS和feathers-sequelize阅读。目前我有一个使用以下代码的工作原型,它返回所需的结果,但仅返回到控制台,以及posts表到/ posts路由的全部内容,但是我只想从表中返回一组特定的记录到/帖子。
该表有一个名为post_status的列,其中帖子可以是“已发布”或“草稿”。我只想将'已发布'的记录返回到/ posts,但是想要实现这个服务器端而不是/ posts?status = published。如何实现这一目标?
见下面的代码:
const path = require('path');
const feathers = require('@feathersjs/feathers');
const express = require('@feathersjs/express');
const socketio = require('@feathersjs/socketio');
const Sequelize = require('sequelize');
const service = require('feathers-sequelize');
const sequelize = new Sequelize('sandbox', 'sandbox', 'secretpassword', {
host: 'localhost',
dialect: 'mysql',
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
},
operatorsAliases: false
});
const Post = sequelize.define('posts', {
post_title: Sequelize.STRING
},
{
timestamps: false,
underscored: true,
});
// Create an Express compatible Feathers application instance.
const app = express(feathers());
// Turn on JSON parser for REST services
app.use(express.json());
// Turn on URL-encoded parser for REST services
app.use(express.urlencoded({ extended: true }));
// Enable REST services
app.configure(express.rest());
// Enable Socket.io services
app.configure(socketio());
app.use(express.errorHandler());
//This works fine to return to the console but not to /posts
Post.findAll({
where: {
post_status: 'published'
}
}) .then(posts => {
console.log(JSON.stringify(posts));
});
//This returns the entire contents of the posts table to /posts
app.use('/posts', service({
Model: Post,
paginate: {
default: 10,
max: 100
},
}));
// Start the server
const port = 3030;
app.listen(port, () => {
console.log(`Feathers server listening on port ${port}`);
});
我尝试了服务中findAll方法的'where',但这并没有改变输出,也没有产生任何错误。
答案 0 :(得分:0)
我通过在服务的find方法中使用where子句解决了这个问题,请参阅下面的完整代码:
{{1}}