2

I am creating a new record like this:

  Resource.create({
    title: req.body.title,
    address: {...req.body.address},
    email: req.body.email,
  }, (err, record) =>{
    if(err){
      res.send({'status': err});
    }else{
      res.send(
        {
          'status': 'it worked',
          'data': req.body
        }
      );
      sails.log(record.title)
    }
  });

The request process perfectly and the new record is added to the database(I can see it too). But I cant get the id right when its created for some weird reason.

I've been following a tutorial and record is supposed to not be undefined, I am using MongoDB with SailsJS

Kevin Hernandez
  • 1,270
  • 2
  • 19
  • 41
  • 1
    U need to use `fetch:true`, so in this example: `Resource.create({ title: req.body.title, address: {...req.body.address}, email: req.body.email, }, (err, record) = > {}, {fetch: true});` – Dejan Kubaša Jul 02 '19 at 06:13

1 Answers1

3

You have to chain a "fetch" after creating the record, for example:

let resource = Resource.create({...}).fetch();

This will fetch the record you just created with its associated id.

Christian Strang
  • 8,470
  • 5
  • 42
  • 53