gpt4 book ai didi

javascript - FeathersJS REST 返回具有特定值的记录

转载 作者:行者123 更新时间:2023-11-29 17:56:14 24 4
gpt4 key购买 nike

我有一个 MySQL 数据库,其中有一个名为“posts”的表,我正在通过 FeathersJS 读取数据。和 feathers-sequelize 。目前我有一个工作原型(prototype),其代码如下,它返回所需的结果,但仅返回到控制台,并将 posts 表的全部内容返回到/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”,但这并没有改变输出,也没有产生任何错误。

最佳答案

我通过在服务的 find 方法中使用 where 子句解决了这个问题,请参阅下面的完整代码:

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,
post_status: 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());


const myService = {
find(params) {
let posts = Post.findAll({
where: {
post_status: 'published'
}
});

return Promise.resolve(posts);
},
get(id, params) {},
create(data, params) {},
update(id, data, params) {},
patch(id, data, params) {},
remove(id, params) {},
setup(app, path) {}
}

app.use('/posts', myService);

// Start the server
const port = 3030;

app.listen(port, () => {
console.log(`Feathers server listening on port ${port}`);
});

关于javascript - FeathersJS REST 返回具有特定值的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48795811/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com