【问题标题】:Function returns the object before, the findById() fill it with the required data函数返回之前的对象,findById() 用需要的数据填充它
【发布时间】:2021-10-29 11:16:47
【问题描述】:

所以,问题在于我认为的 javascript 基础知识。

'use strict';

module.exports = function accountWithSingleOrganisation(Account) {

  Account.accountWithSingleOrganisation = function(accessToken, cb) {

    accessToken.user(function(err, user) {
      if (err) return cb(err);

      let accData = {};
      user.role(async (err, role) => {
        if (err) return cb(err);

        for (let it in role.scopes) {
          if (role.scopes[it].account == null) break;
          console.log('1');
          await Account.findById(role.scopes[it].account, (err, account) => {
            if (err) return cb(err);

            console.log('2');

            // Assigning Data Here
            accData = Object.assign(accData, {                      
              name: account.name,
              id: account.id,
            });
          });
        };
        console.log(accData);
        return cb(null, accData);
      });
    });
  };
};

在我的程序中,我不知道如何正确使用async await,因此执行了 for 循环,并且函数甚至在分配数据之前就返回了 accData。你能告诉我如何使用异步等待,或者任何其他方式,我的函数首先将它的数据分配给对象,然后返回数据。

【问题讨论】:

    标签: javascript node.js mongodb loopback3


    【解决方案1】:

    我们,可以在我们使用 await 的地方进行响应,然后推送数据。这样就可以了。

    【讨论】:

      猜你喜欢
      • 2020-01-01
      • 2018-11-23
      • 1970-01-01
      • 2016-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-11
      相关资源
      最近更新 更多