是否可以在远程挂钩中执行 models.find() ?

Is it possible to do models.find() inside a remote hooks?

我有一个关于环回 js 的问题,特别是环回 3。是否可以在远程挂钩中执行 models.find() 操作?

我试图在 afterRemote() 远程挂钩中发出 models.find() 请求,但是我没有不知道如何获得 find() 的响应,甚至不知道操作是否成功。

module.exports = function(User) { 
const app = require('../../server/server');
const models = app.models;

User.afterRemote('find', function(context, user, next){
    models.saldo_cuti.find(function(err){
      if (err) throw (err);
      return next(); //this only return regular User.find()
    });
  })
}

我希望能够操纵 models.saldo_cuti.find() 结果,但我似乎找不到如何操作。

您的模型对象及其方法等可以运行在任何地方。处于该操作挂钩内不会改变它们。

看起来你只是没有对结果做任何事情。 function(err) 缺失 function(err, result)。仅供参考,您可以使用 async/await 让这些东西更容易处理:

module.exports = function(User) { 

    User.afterRemote('find', async (context, user) => {
        const docs = await User.app.models.saldo_cuti.find();
        // do something with docs, or apply a filter to find() to limit results.
    });

};