1

I have a problem where I am creating a user and want to save some items associated to this user at the same time.

I have managed to get it where I can create an item and reference the user_id associated to that item but I cant work out how to push all the items the user has into the User schema.

I have tried looping through req.body.taken and adding to user schema but I just get null returned.

router.post('/data', async (req, res) => {


  var user = new User({
    name: req.body.name,
    website: req.body.website,
  })

  user.save(function (err) {
    if (err) {
      return next(err);
    }
  })

  req.body.taken.forEach((item) => {

    var item = new Item({
      x: item.x,
      y: item.y,
      xSize: item.xSize,
      ySize: item.xSize,
      user: user,
      imageSource: item.imageSource,
      user: user
    }
    )

    item.save(function (err) {
      if (err) {
        return next(err);
      }
    })



  })

const UserSchema = new Schema(
  {
    id: Number,
    name: String,
    website: String,
    items: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Item'
      }]

  },
  { timestamps: true }
);

const ItemSchema = new Schema(
  {
    id: Number,
    x: Number,
    y: Number,
    xSize: String,
    ySize: String,
    imageSource: String,
    user: { type: mongoose.Schema.ObjectId, ref: 'User' }
  },
  { timestamps: true }
);


  User.find({})
      .populate('items')
      .exec(function(error, items) {
          console.log(items)
      })


When I call User find I want to get all items associated to that user (which will be an array as req.body.taken is an array of items.

2
  • your router function is no where ending also clearly and systematically mention what you want to achieve. Commented May 18, 2019 at 10:56
  • Above code has lots of bugs. Please try to resolve those like : 1. next() callback is no where declared. 2. var item = use different variable name here because hoisting will create problem. Commented May 18, 2019 at 11:00

1 Answer 1

1

There's a few issues here.

  1. you are not waiting for user.save, theres a good chance all your items are being saved before the user doc is even returned. you should either await for promise on user.save or put the items.forEach loop in the callback. also you are saving the entire objet and not the user._id returned which mongoose wont allow as it does not match the scheme. making the creation fail.

  2. You are not saving the items array in your user, how can you expect to populate that field if its empty. the flow of your code needs to change a little in order to allow you to update this field.

Sign up to request clarification or add additional context in comments.

1 Comment

What I find confusing is that at the beginning the User does not exist however I need to save the reference to an item which does not exist either yet. Do I save a ref to the item when I create the user?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.