0

i am new to using easymongo and mongodb.

I have the following code that save a record to a database

var id;
chatRooms.save(chatData).then(function(res){
  console.log("save chat", res);
  id = res._id;
});

now if i log id to the console i get undefined, but if i log it withing the save function i get the ID.

I know that this is because what is returned is a promise but i don't know what to do with it. All i want is to be able to store the ID of the new record.

Thanks.

ragebunny
  • 1,582
  • 10
  • 33
  • 53
  • If you want the data in your `then`, you should return a promise and `resolve(chatData)` inside the `save` function. **Edit:** @DerekBrown , why? https://jsfiddle.net/jmj9sj6k/ – blex Oct 23 '17 at 22:07
  • @blex this doesn't work. This is not inside a promise context. – Derek Brown Oct 23 '17 at 22:08
  • the function inside the `.then()` call is a callback. Because it executes asynchronously, your code has a race condition. – Derek Brown Oct 23 '17 at 22:11
  • 1
    did you search in SO? – Igor Soloydenko Oct 23 '17 at 22:11
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Igor Soloydenko Oct 23 '17 at 22:11
  • take a look at this https://stackoverflow.com/questions/46880371/object-from-giphy-api-prints-nothing/46880547#46880547 – Derek Brown Oct 23 '17 at 22:12
  • @blex because there is no `new Promise()` in his code. Therefore `resolve()` is undefined. – Derek Brown Oct 23 '17 at 22:13
  • @DerekBrown You don't know that. He did not show his code for the `save` function. And he does mention a Promise being returned – blex Oct 23 '17 at 22:14

2 Answers2

1

All code that uses the variable id must be executed within the callback function: var id;

chatRooms.save(chatData).then(function(res){
  console.log("save chat", res);
  id = res._id;

  console.log(id);
  //Any other stuff using the variable id
});

So instead of trying to save it outside the function, put the code that uses it within the function. Otherwise it will not work, because the .save takes time, and it is asynchronous so otherwise the code will go on without it.

Ethan
  • 3,410
  • 1
  • 28
  • 49
0

Javascript is not a sequential language per se. When you use promises and callbacks, the execution order goes out the window. In this instance if you want to use the resulted ID, you will have to continue your code inside the then's function.

var doSomethingWithId = function(id) {
  doSomething()
};

chatRooms.save(chatData).then(function(res){
  console.log("save chat", res);
  doSomethingWithId(red._id);
});

Unfortunately there's not many ways around it.

You could use ES6's async/await syntax to streamline the code, but that will introduce a lot of other problems (transpiling, etc).

To understand how the language works, I recommend watching this video: https://www.youtube.com/watch?v=8aGhZQkoFbQ

meza
  • 8,247
  • 1
  • 14
  • 23