首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有特定值的FeathersJS REST返回记录

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

Stack Overflow用户
提问于 2018-02-14 20:40:51
回答 1查看 386关注 0票数 0

我有一个MySQL数据库,其中有一个名为“posts”的表,我是从FeathersJS羽毛-后遗症上读取的。目前,我有一个具有以下代码的工作原型,它返回所需的结果,但只返回控制台,还将posts表的全部内容返回到/posts路由,但是,我只想从表中将一组特定的记录返回给/posts。

该表中有一个名为post_status的列,其中一篇文章可以“发布”或“草稿”。我只想将“已发布的”记录返回给/posts,但希望实现这个服务器端,而不是/posts?status=published。如何才能做到这一点?

见下文代码:

代码语言:javascript
复制
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”,但是这并没有改变输出,也没有产生任何错误。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-17 17:44:28

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

代码语言:javascript
复制
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}`);
});

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48795811

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档