Node.js 如何对待feathersjs挂钩中的承诺?

Node.js 如何对待feathersjs挂钩中的承诺?,node.js,promise,feathersjs,feathers-sequelize,feathers-hook,Node.js,Promise,Feathersjs,Feathers Sequelize,Feathers Hook,我想在插入数据库之前验证数据。Feathersjs的方法是使用挂钩。在插入一组权限之前,我必须考虑用户帖子提供的数据的完整性。我的解决方案是查找与用户提供的数据关联的所有权限。通过比较列表的长度,我可以证明数据是否正确。吊钩代码如下所示: const permissionModel = require('./../../models/user-group.model'); module.exports = function (options = {}) { return function

我想在插入数据库之前验证数据。Feathersjs的方法是使用挂钩。在插入一组权限之前,我必须考虑用户帖子提供的数据的完整性。我的解决方案是查找与用户提供的数据关联的所有权限。通过比较列表的长度,我可以证明数据是否正确。吊钩代码如下所示:

const permissionModel = require('./../../models/user-group.model');

module.exports = function (options = {}) { 
  return function usergroupBefore(hook) {
    function fnCreateGroup(data, params) {
      let inIds = [];
      // the code in this block is for populating the inIds array

      if (inIds.length === 0) {
        throw Error('You must provide the permission List');
      }
      //now the use of a sequalize promise for searching a list of
      // objects associated to the above list
      permissionModel(hook.app).findAll({
         where: {
          id: {
            $in: inIds
          }
       }
      }).then(function (plist) {
        if (plist.length !== inIds.length) {
          throw Error('You must provide the permission List');
        } else {
          hook.data.inIds = inIds;
          return Promise.resolve(hook);
        }
      }, function (err) {
        throw err;
      });
    }

    return fnCreateGroup(hook.data);
  };
};
我对处理其他参数的某些信息的行进行了注释,以填充
INID
数组。我还使用sequalize搜索与存储到数组中的信息相关联的对象

该块位于
内,然后在后台执行
块。在feathersjs控制台上显示结果

然而,数据被插入到数据库中


如何从FeatherJS钩子中执行的承诺返回数据?

您的
fnCreateGroup
没有返回任何内容。您必须
返回permissionModel(hook.app).findAll
。或者,如果您使用的是Node 8+,则更容易执行此操作:

const permissionModel = require('./../../models/user-group.model');

module.exports = function (options = {}) { 
  return async function usergroupBefore(hook) {
    let inIds = [];
    // the code in this block is for populating the inIds array

    if (inIds.length === 0) {
      throw Error('You must provide the permission List');
    }

    //now the use of a sequalize promise for searching a list of
    // objects associated to the above list
    const plist = await permissionModel(hook.app).findAll({
        where: {
        id: {
          $in: inIds
        }
      }
    });

    if (plist.length !== inIds.length) {
      throw Error('You must provide the permission List');
    } else {
      hook.data.inIds = inIds;
    }

    return hook;
  };
};

我正在将节点6更新为节点8安装。谢谢