Sampsa
Sampsa

Reputation: 711

Return value coming through as undefined

If I console.log the output from count+1, I get a correct number value. If I output the value of note.note_id, I get undefined. Why is this?

I have tried setting value to a predefined one inside the function.

note.note_id = db.notes.count(function(err, count) {
  return count + 1;
});

Upvotes: 0

Views: 61

Answers (1)

ThiefMaster
ThiefMaster

Reputation: 318518

Hard to answer without knowing what db.notes is but it seems to be something accessing a database. This means it's most likely asynchronous which meansa that the count() method will never return a value but you need to do whatever you want to do with the result inside the callback.

db.notes.count(function(err, count) {
    note.note_id = count + 1;
    // do more stuff here
});
// do NOT do stuff here. it will run BEFORE the callback has been executed

Upvotes: 3

Related Questions