如何对待羽毛球内的承诺?

我想在插入数据库之前validation数据。 Feathersjs的方式是通过使用钩子。 在插入一组权限之前,我必须考虑用户post提供的数据的完整性。 我的解决scheme是find与用户提供的数据相关的所有权限。 通过比较列表的长度,我可以certificate,如果数据是正确的。 钩子的代码贴在下面:

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); }; }; 

我评论了处理一些其他参数信息的行来填充inIds数组。 我还使用sequalizesearch与存储在数组中的信息关联的对象。

当前块内的这个块在后台执行。 在feathersjs控制台显示结果

代码执行

但是,数据被插入到数据库中。

我如何从一个在feathersjs钩子内部执行的promise中返回数据?

你的fnCreateGroup没有返回任何东西。 你必须return permissionModel(hook.app).findAll 。 或者,如果您使用的是节点8+ asynchronous/等待将使这更容易遵循:

 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; }; };