简体   繁体   English

Mongoose:如何将之后创建的文档中的数据传递到之前创建的文档中?

[英]Mongoose: How do I pass data from a document created afterwards into a document created right before?

In my signup request, I use create two documents (in separate collections) based on two different schemas: a User model and a Client model.在我的注册请求中,我使用基于两个不同模式创建两个文档(在单独的集合中):用户模型和客户端模型。 For context, a client will be one object referencing an array of many Users.对于上下文,客户端将是一个引用多个用户数组的对象。

The User scheme includes 'clientID' field, which should hold the User's Client's ._id.用户方案包括“clientID”字段,该字段应保存用户客户端的 ._id。 Likewise, the Client's 'users' field would include an array of Users attached.同样,客户的“用户”字段将包括附加的用户数组。 The latter works OK.后者工作正常。

On signup, I create a User, and then a Client.在注册时,我创建一个用户,然后创建一个客户端。 I am able to pass the User._id into the Client's array of users no problem.我能够将 User._id 传递到客户端的用户数组中,没问题。 But, how do I get the Client's ._id into the User's clientID field?但是,如何将客户端的 ._id 放入用户的 clientID 字段?

The code below errors saying: Cannot access 'client' before initialization - I understand why this is happening because of the order of code.下面的代码错误说:在初始化之前无法访问“客户端” - 我理解为什么会因为代码顺序而发生这种情况。

But how do I get my code to reciprocate so that I can add the Client._id to the User's clientID field?但是我如何让我的代码进行交互,以便我可以将 Client._id 添加到用户的 clientID 字段? I am sure this is a common problem, but I can't find the relevant doc on Mongoose's docs.我确定这是一个常见问题,但我在 Mongoose 的文档中找不到相关文档。

If anyone could help?如果有人可以帮忙吗? Many thanks!非常感谢!

 module.exports.signup = async (req, res) => { // extract data from the req.body const { email, password, fName, lName, companyName, teams } = req.body; try { // create a new user on the User model const user = await User.create({ email: email, password: password, clientID: client._id, }); // create a new client on the Client model const client = await Client.create({ companyName: companyName, users: user._id, }); res.status(201).json(user); } catch (err) { const errors = handleErrors(err); res.status(400).json(errors); } };

You can instead use a specific objectId for the client when you create it and reference it later.您可以在创建客户端时为客户端使用特定的objectId ,并在稍后引用它。

Just copy past the whole code:只需复制整个代码:

const {ObjectID} = require('mongodb');

module.exports.signup = async (req, res) => {

  // extract data from the req.body
  const { email, password, fName, lName, companyName, teams } = req.body;

  try {

    const clientId = ObjectID();

    // create a new user on the User model
    const user = await User.create({
      email: email,
      password: password,
      clientID: clientId,
    });

    // create a new client on the Client model
    const client = await Client.create({
      companyName: companyName,
      users: user._id,
      _id: clientId
    });

    res.status(201).json(user);
  } catch (err) {
    const errors = handleErrors(err);
    res.status(400).json(errors);
  }
};

Another solution is that you can create a user without clientId and after you create client you can update the user with the created client.id :另一个解决方案是您可以创建一个没有clientIduser ,在创建client您可以使用创建的client.id更新user

const user = await User.create({
  email: email,
  password: password,
  // clientID: clientId,
});

// create a new client on the Client model
const client = await Client.create({
  companyName: companyName,
  users: user._id,
});

user.clientId = client._id;
await user.save();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM