cmar
1
Im a bit confused if i am writing code and using mongoose how do i write code so the following database insertion will be performed?
const createAndSavePerson = (done) => {
let carlSee = new Person({name: "Carl See", age: 34, favoriteFoods: ["eggs", "steak", "bread"]});
carlSee.save(function(err, data) {
if (err) return console.error(err);
console.log("Document inserted succussfully! :" + data);
done(null, data)
});
};
myApp.js - boilerplate-mongomongoose (2) - Replit
I don’t understand the question. Are you asking why/how the code works?
cmar
3
I want to write code that performs the database insertion without running the tests
You call the function and provide it with the done
callback.
createAndSavePerson((err, data) => {
if (err) console.error(err);
console.log(data);
});
cmar
5
Alright thanks i didn’t realize i needed a callback to call the function since there is already a callback in the function itself
cmar
6
I have tried calling the model find function using the following but i dont get any results in the console.
findPeopleByName("Frankie", (err, data) => {
if (err) return console.error(err);
console.log(data)
});
cmar
7
Nevermind its because i never reran the function to insert many people i just assumed they were already in the database