如何在 API 中的 res.send() 中发回两个对象

Dev*_*jee 0 javascript mongoose node.js express

我有一个这样的获取 API

router.get('/exist', async (req, res) => {
  try {
    const { user: { _id: userId } } = req;
    const user = await User.findById(userId);
    const profile = await Profile.findById(user.profile, 'role admin');
    const admin = await Admin.findOne({ _id: profile.admin })
    res.send(admin.role,profile);
  } catch (e) {
    res.status(500).send();
  }
});
Run Code Online (Sandbox Code Playgroud)

admin只有一个领域一个要发送回叫的作用。这就是为什么在 res.send() 我写了admin.role.

所以基本上我想这两个领域发回一起admin.roleprofile

直到现在我已经尝试与合并

Object.assign(profile,admin)
Run Code Online (Sandbox Code Playgroud)

&

{...profile,...admin}
Run Code Online (Sandbox Code Playgroud)

这两个都不起作用。

我该怎么做呢?

Moh*_*ony 5

创建新对象,如

 res.send({'Role':admin.role,'profile': profile});
Run Code Online (Sandbox Code Playgroud)