Thank you @RandellDawson.
Unfortunately, it’s still not clicking for me. I went ahead and looked at the answer for this challenge in order to draw connections between what I think I know about JS and callbacks and what you are showing me.
Might help me to try and talk this out a little bit (not that you need to keep answering me)
createAndSavePerson
is a variable it’s value will be whatever is to the right of the ‘=’
The expression is an arrow function
which would be equivalent to function fname()
done
is an argument for these arrow functions.
done
is a function itself.
When createAndSavePerson
is evaluated, its value is the return value from the arrow function. Since it is a function that is being passed into the arrow function, the value of createAndSavePerson
is the return value of the done()
(uncertain, is a question below).
If assuming the above is correct so far, then seems like createAndSavePerson
is either err
or res.json
for that person that was created (from your code snippet).
Questions:
- Is the return value of the arrow function the return value of the
done()
- Is the
/*, data*/
supposed to be uncommented?
- Is
/*, data*/
commented out because if it wasn’t then we we get (below) from the start of these exercises ?
Continuing On…
If I am correct so far, then I know that within any function I can do stuff
so all of my work would need to come before done
is called. For this example, that means I need to write code for a new Person
to be created and save
that person in the db.
I don’t want to copy the solution code here, but I have looked at it. The save
is odd to me and like others on that thread, I am struggling to confidently know what data
is.
From the Mongoose docs, I do see that .save()
can take a callback function as a parameter. The solution code appears to be doing that but not sure I follow.
My thought to this point (and I can try it but want to continue the thought process here first) is that I should be able to:
(async is needed as well)
let data = await myNewPerson.save();
done(null, data);
After the function, I did call it with createAndSavePerson();
This works when it comes to saving the person to the DB, but I get an error in the console so I am not actually sure that me calling the function as I did was correct.
TypeError: done is not a function
This kind of makes sense because I didn’t pass a function into the createAndSavePerson
call. Considering this, I ran it again but this time I passed console.log
as the function.
createAndSavePerson(console.log);
I didn’t get any errors this time but I have not submitted the challenge to see if it passes or not.