0

I have a Person model

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const PersonSchema = new Schema({
name: String,
cars: [{
    type: Schema.types.ObjectId,
    ref: 'Cars'
    }]
  });

const Person = module.exports = mongoose.model('Person', PersonSchema);

and I have a Cars model

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const CarsSchema = new Schema({
color: String,
owner: {
    type: Schema.Types.ObjectId,
    ref: 'Person'
},
});

const Cars = module.exports = mongoose.model('Cars', CarsSchema);

How do I make sure that every time a car is added, it get listed in a particular person's car array?

Right now, I've done something like this:

  const newPerson = new Person({
    name: 'jared'
  });

  newPerson.save()
    .then(person => {
      console.log(person);
    });

  const newCar = new Car({
    color: 'red'
  });

  newCar.save(function(err, car) {
    if (err) {
      console.log(err)
    } else {
      car.populate(
        car, {
          path: "owner"
        },
        function(err, car) {
          if (err) {
            console.log(err);
          } else {
            console.log(car);
          }
        });
    }
  });

the code works without errors, and the car gets properly printed to the terminal with the "jared" person document seen occupying the owner field, but the result isn't saved to MongoDB. Instead, when I check MongoDB, I just see the car with only the "jared" document _id occupying the owner field. Can anyone please tell me why that is?

Toan Tran
  • 1,937
  • 1
  • 24
  • 37
  • `person.save()` returns a promise, but the remainder of your listing in which you try to access that created item is executing before the async function completes. – Neil Lunn May 25 '17 at 08:35
  • could you point me to a link that helps solve the problem please? Because I have changed and run this code again and again. I've read the mongoose documentation, and after modelling my two schema to match the documentation guidelines the app crashed instead and I got a bunch of mongodb errors – Emmanuel Wayne May 25 '17 at 12:34

1 Answers1

0

You have to assign _id from the person to car owner.

let newPerson = new Person({
  name: 'jared'
});

newPerson.save()
  .then(person => {
    console.log(person);

    let newCar = new Car({
      color: 'red',
      owner: person._id
    });

    newCar.save(function(err, car) {
      if (err) {
        console.log(err)
      } else {
        car.populate(
          car, {
            path: "owner"
          },
          function(err, car) {
            if (err) {
              console.log(err);
            } else {
              console.log(car);
            }
          });
      }
    })
  });
Toan Tran
  • 1,937
  • 1
  • 24
  • 37
  • oops, I forgot to add, yes I actually assigned the _id from the person to the car owner... but it just saves with only the owner _id – Emmanuel Wayne May 25 '17 at 09:01
  • No, it does not only save `owner._id`. It saved a reference to `Person object`. Please read MongoDB docs for more details. [Mongoose Population](http://mongoosejs.com/docs/populate.html) – Toan Tran May 25 '17 at 09:04