Can someone explain me why my solution is not working

Tell us what’s happening:

It is nearly the same as the fcc solution but they used promises, mine uses async with try catch blocks,
thank you

Your code so far

var findEditThenSave = async function(personId, done) {
  let foodToAdd = 'hamburger';

  try{
    let person = await Person.findById(personId)
    let newData = person.favoriteFoods.push(foodToAdd)
    let updated = await person.update(newData, {upsert: true })
    done(null, updated)
  } catch(err) {
    console.error(err)
  }
};

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36.

Challenge: Perform New Updates on a Document Using model.findOneAndUpdate()

Link to the challenge:

If you have an error thrown from try/catch, what is the error?

If you have no error, then append an explain to your query and compare against the explain received from the “thenable”(none await/async) solution.

How to use explain?
Like this:

const explain = await Character.find({ name: /Picard/ }).explain().
  then(res => res[0]);
// Object describing how MongoDB planned to execute the query
explain.queryPlanner;
// Object containing stats about how MongoDB executed the query
explain.executionStats;

or https://masteringjs.io/tutorials/mongoose/explain#:~:text=Jun%203%2C%202020,query%20into%20an%20explain()%20.

If you deal with mongodb you have to use explain now and then same as you do have to use console.log if you deal with JavaScript.

Also could be the case that the test running your code may not be capable to transpile or know how to test around async/await.

1 Like

Welcome, mootarek.

Whilst what you have done does likely find, edit, and update the record, the whole point of the lesson is:

Perform New Updates on a Document Using model.findOneAndUpdate()

Also:

You should return the updated document.

So, you cannot expect the tests to pass, if you do not use the expected methods.

Hope this clarifies.

1 Like

yes, that clarifies a lot thank you so much.