I haven’t actually tried to make this work for the exercise , but i’m wondering why a new Person is being declared when the personSchema is already referencing a new Schema to be created . To me, it looks like only the following should be enough to add the object to the dB
const data = {name: 'Ben', age: 23, favouriteFoods: ['tuna', 'bread']}
Person.create(data,(err,newPerson)=>{
if(err){throw err}
console.log(newPerson, " Successfully Added to dB!")
})
Again, apologies if this doesn’t directly address the question, since I haven’t had a chance to look at the exercise, but if the point is just to add a new person per the given schema , shouldn’t the above suffice?
I think the only purpose of the “personSchema” was to use it to create the “Person” model. In order to create a document you need to use “new Person” or “Person.create” as you mentioned. Its my understanding that calling “new Person” and then calling “save” essentially does the same thing as using “Person.create” (Constructing documents).
Based on that logic I think that your snippet essentially does the same thing as mine except that the exercise requires you to use the “done” callback so the tests will work.
Thanks for your feedback. Let me know if anything I said sounds wrong.
Ah, good to know @JMagers i’ve always used the .create method rather than the .save or maybe come to think of it I may have used it sparingly for saving authenticated users before , anyway a potential issue that I see in your code not passing maybe that your ternary operator is not returning any results, so maybe you can try:
If that doesn’t solve it, then see if the solution above @dnlnav works, if it does it must be related with the ternary operator as that is the only difference I see and you may need to refactor to see what’s up with your ternary operator
I refactored the code to avoid using the ternary operator like you suggested.
var createAndSavePerson = function(done) {
const person = new Person({name: 'Joe', age: 21, favoriteFoods: ['pizza', 'burger']});
person.save((err, data) => {
if (err) {
return done(err);
}
return done(null, data);
});
};
Unfortunately the tests still result in the same timeout failure that the previous code did. This should be identical to the solution provided by @dnlnav but it still doesn’t work. I’m wondering if maybe there is a bug in the test itself. Thanks again.
ok @JMagers , had a chance to setup and go thru the lessons, the tests passed for me so I don’t think there is any bug in the tests, fyi the OP has a spelling error in favoriteFoods that won’ allow the tests to go thru, anyway here is the entire code that passed for me with very minimal change to the last code you posted
Unless there is a difference in what you have written before the createAndSavePerson function, which I haven’t seen so can’t tell, other than that don’t know what to tell ya…
I got the tests to pass! Thanks to your help I was able to eliminate the possibility that the problem had to with any of that code. Apparently the password for my mongo database had some special characters in it that are not allowed in a URI. I would of thought that this kind of error would have been caught in the earlier tests but apparently not.
hmmm… that’s weird because a db connection string error should have shown in your glitch log , you can access it by clicking log on the top left of your screen (If you haven’t done so already) . Anyway, good that you solved it…
This was a lifesaver. I didn’t realize you had to include the “<” and “>” when you replaced that substring with your password until I saw this and read more closely. Thanks!
I had to change the password once in MongoDB and in the .env-file and then it worked. I assumed the first challenge will check the database connection, but apparently it doesn’t.