0

I am querying a database asynchronously and getting the result.

const games=await new Promise(res=>{Game.find({ids:id},(err,doc)=>res(doc))})
console.log(games[0])

When I print the result I get the following

{ gameId:'8636f1b196f1c6aa164341be448126ba',
  done: '1',
  score: [],
  finishedAt: '' }

But then when I do console.log(games[0].done) I get undefined whereas the expected output is 1

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
m0bi5
  • 8,900
  • 7
  • 33
  • 44
  • There is no JSON in your question. (And if what you've quoted were used as JSON, it would be invalid JSON.) JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder May 29 '18 at 08:29
  • 2
    Show the schema as there is likely a mismatch between the document and schema. BTW `const games = await Game.find()` works fine as mongoose methods already return a Promise. – Neil Lunn May 29 '18 at 08:31
  • @NeilLunn thanks, I hadnt defined it as a field in my Schema – m0bi5 May 29 '18 at 08:39
  • 1
    As stated, this will be a schema mismatch. You can fix the schema or work around it with `toObject()` or adding `lean()` to the query i.e `Game.find(...).lean()`. Just note that one answer added here was a bit better at "using google" since it's just content gleaned from one of the "over involved" approaches to an answer there. – Neil Lunn May 29 '18 at 08:39

0 Answers0