【问题标题】:Nothing returned in feathersjs before hook sequelize association在钩子续集关联之前,feathersjs 中没有返回任何内容
【发布时间】:2020-05-12 13:25:54
【问题描述】:

我在我的用户服务中创建了一个 before 挂钩,旨在包含来自组织模型的记录。用户与组织的关系是

users.belongsToMany(models.organizations,{through: 'users_to_organizations'});

before钩子叫做attach-orgnaization.js,看起来像

module.exports = (options = {}) => {
  return async context => {
    const organizations = context.app.services.organizations.Model;
    context.params.sequelize = {
      include: [{model: organizations}]
    };
    return context;
  };
};

像这样在 users.hooks.js 中设置

....
const attachOrganization = require('../../hooks/attach-organization');
module.exports = {
  before: {
    all: [],
    find: [ authenticate('jwt') ],
    get: [authenticate('jwt'), attachOrganization()],
....

当我 GET /users?id=1 时,我让用户返回,但对用户在 users_to_organizations 中关联的组织一无所知,这恰好是组织 3“测试组织”。

我希望在回复中看到更多与组织相关的字段。

我只看到了

{
    "total": 1,
    "limit": 10,
    "skip": 0,
    "data": [
        {
            "id": 1,
            "email": "hello@feathersjs.com",
            "googleId": null,
            "githubId": null,
            "createdAt": "2020-04-29T04:26:49.541Z",
            "updatedAt": "2020-04-29T04:26:49.541Z"
        }
    ]
}

单步调试器(pycharm)我可以看到钩子里的代码正在执行。

我猜要么 sequelize 没有看到关系,要么羽毛没有将包含添加到查询中。

我可能缺少什么?

谢谢!

【问题讨论】:

  • 问题是我使用查询字符串来指定 ID。虽然这会返回用户,但它似乎不会导致 feather-sequelize 对 include 参数做任何事情。使用/users/1,它确实应该是返回组织和用户。

标签: sequelize.js feathersjs feathers-sequelize feathers-hook


【解决方案1】:

在feathersjs中,Get方法有2种

  1. Get:获取特定数据。例如:GET /user/:id(id 是用户 id)
  2. Find :使用过滤器获取所有数据,例如:GET /user?name=xyz(它将返回所有名称为 xyz 的文档)

您已在 get 方法中附加了 attachOrganization() 并调用 /users?id=1 这是 FIND 方法。所以要么通过 /users/1 调用它 id 1 是您的 _id 或将 attachOrganization() 添加到 @ 的钩子中987654330@方法也是

【讨论】:

    猜你喜欢
    • 2020-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-30
    • 2013-04-27
    • 2015-10-09
    • 2016-07-24
    相关资源
    最近更新 更多