MongoDB and Mongoose - Create and Save a Record of a Model

Tell us what’s happening:
what am I doing wrong? How do I fix the error?
require(‘dotenv’).config();

const dotenv = require(“dotenv”);
dotenv.config({path:“sample.env”});

let mongoose = require(“mongoose”);

mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true});

let peopleSchema = new Schema({
name : {type: String, required: true},
age : Number,
favoriteFoods : [String]
});

let Person = mongoose.model(“Person”, personSchema);

var createAndSavePerson = function(done) {
var createAndSavePerson = function(done) {
var janeFonda = new Person({name: “Jane Fonda”, age: 84, favoriteFoods: [“eggs”, “fish”, “fresh fruit”]});

janeFonda.save(function(err, data) {
if (err) return console.error(err);

done(null/, data/)
});
};

Your project link(s)

solution: https://boilerplate-mongomongoose.tffree.repl.co

Your browser information:

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

Challenge: MongoDB and Mongoose - Create and Save a Record of a Model

Link to the challenge:

Why did you add

var createAndSavePerson = function(done) {

twice?


I assume you have gone through the JS curriculum? If not you should really do so before moving to the backend part of the curriculum.

Yes, I already went through the JS curriculum

https://boilerplate-mongomongoose.tffree.repl.co
What did I do wrong now?
require(‘dotenv’).config();

const dotenv = require(“dotenv”);
dotenv.config({path:“sample.env”});

let mongoose = require(“mongoose”);

mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true});

let peopleSchema = new Schema({
name : {type: String, required: true},
age : Number,
favoriteFoods : [String]
});

let Person = mongoose.model(“Person”, personSchema);

function createAndSavePerson(done) {
var janeFonda = new Person({ name: “Jane Fonda”, age: 84, favoriteFoods: [“eggs”, “fish”, “fresh fruit”] });

janeFonda.save(function(err, data) {
    if (err)
        return console.error(err);

    done(null /*, data*/);
});

}

  • how do you create a “new” instance?
  • what is Schema here?!

address those changes and it should be alright? happy learning :slight_smile:

https://boilerplate-mongomongoose.tffree.repl.co
let personSchema ({
name : {type: String, required: true},
age : Number,
favoriteFoods : [String]
});

let Person = mongoose.model(“Person”, personModel);

function createAndSavePerson = function(done) {

let francesca = new Person({name : ‘Francesca’, age: 20, favoriteFoods : [‘sushi’]})

francesca.save((error, data) => {

if(error){
console.log(error)
}else{

}
done(null , data);
});
now what did I do wrong?

I would suggest you read the article that is linked to.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.