0

i use sails js 0.12.14 with mongodb. if i try to get a document after insert the document the document is empty

Products.create(product).exec(function(err, createdProduct) {
    Products.find({_id : createdProduct.id}).exec(function(err, foundProductAfterCreate) {
        console.log(foundProductAfterCreate);
    })});

Can anybody explain why the document is not available?

Update: This is the correct code for me...

Products.create(product).exec(function(err, createdProduct) {
    Products.find({_id : createdProduct[0].id}).exec(function(err, foundProductAfterCreate) {
        console.log(foundProductAfterCreate);
    })});

1 Answers1

2

The doc you are querying for is literally the same one you already have

Products.create(product).exec(function(err, createdProduct) {
    // handle the error...
    console.log(createdProduct); // the full record
});

createdProduct is exactly the same object you would get if you queried by id.

If you do ever need to query by id, sails does a fairly comprehensive switch from _id which is the mongo standard, to id with no underscore. You would do that like this:

Products.findOne({id: createdProduct[0].id}).exec(function(err, foundAgain) { // etc

...no underscores anywhere unless you use .native for more basic mongo query access.

arbuthnott
  • 3,819
  • 2
  • 8
  • 21