I'm trying to create a one to many relationship using Node JS and Mongoose. I have the following:
- an object for a company (with all the details about it and also the owner of this company. First, the owner will be an ID, given using a POST request).
- an object for the owner (found by the ID given in the first step)
What I want to introduce in the database:
I want to reuse the object for the company, and change the owner id with the owner object, and then to introduce it in the database. The problem is that when I want to change the value for the owner, it remains the same.
This is the company object:
var newCompany = new Company({
owner: req.body.owner,
name: req.body.name,
email: req.body.email,
phone: req.body.phone,
web: req.body.web
})
I have this:
const companyOwner = await User.findById(newCompany.owner)
newCompany.owner = companyOwner
So whant can I do to finish this task?
Thank you!