如何在Promise中结合2个对象模型?

时间:2019-04-10 03:41:18

标签: express object mongoose promise

我有2个对象(Account和AccountMeta),当我加入console.log时,它可以工作,但是当我在res.json上获得最终结果时,只有1个数据出现,其余进入错误范围,

我的代码有什么问题?

这是我的代码

 exports.get_Account = async (req, res) => {
  const accounts = await Account.find();

  accounts.map(account => {
    AccountMeta.find({ account_id: account._id }, err => {
      if (err instanceof mongoose.Error.CastError) {
        return res.status(422).send({
          error: 'Something wrong with your ID, please check ' + err.message
        });
      }
    })
      .then(resultMeta => {
        console.log('resultMeta \n ', resultMeta);
        console.log('account \n ', account);
        const new_account = {};
        resultMeta.map(editMeta => {
          new_account[editMeta.key] = editMeta.value;
        });
        // console.log('new account \n ',new_account)

        let dynamicAccount = Object.assign({}, account._doc, new_account);
        console.log('dynamic account \n ', dynamicAccount)
        res.json(dynamicAccount);
      })
      .catch(err => {
        console.log('err \n');
      });
  });
};

0 个答案:

没有答案