How to add reference of one schema to another in Mongoose and inserting data by json? How to add reference of one schema to another in Mongoose and inserting data by json? mongoose mongoose

How to add reference of one schema to another in Mongoose and inserting data by json?


user.wallet = req.body.wallet won't work.

As far as User is concerned user.wallet is just a simple field of the type ObjectId that store a 24 char hex string-like object: 522abc....

This "reference" is actually a higher level interpretation of Mongoose.

That's why you can't directly do user.wallet = req.body.wallet. You have to do something like this:

var user = new User();…// user.wallet = req.body.wallet; // won't workvar wallet = new Wallet(req.body.wallet) // because Wallet too is a Schema just like Useruser.wallet = wallet.id;…