Create and Save a Record of a Model. It accepted my project with checkmark however it didn't count on the record. It says Test timed out

var mongoose = require("mongoose");
var Schema = mongoose.Schema;

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

var Person = mongoose.model('Person', personSchema);

var document = new Person({
  name: "Mehmet",
  age: 24,
  favoriteFoods: ['Apple', 'Banana']
});


var createAndSavePerson = function(done) {
    document.save(function(err,data){
      if (err) return console.error(err);
      console.log(document.name + " likes the food");
      
    });

};

//----- **DO NOT EDIT BELOW THIS LINE** ----------------------------------
exports.PersonModel = Person;
exports.createAndSavePerson = createAndSavePerson;

@mehmetko95 Do you have a screenshot showing the checkmark but Test timed out message?

Also, can you post a link to your Glitch project, so we can test somethings out?

Hi!
The definition of your document object should be inside the createAndSavePerson function. Only that function gets exported in the last two lines. So when your code is tested the document object would be undefined.

so I can do like this :slight_smile:
const createAndSavePerson = function(done){
var document = new Person({
name: “John”,
age: 24,
favoriteFoods: [“Pizza”,“Sushi”]
});
document.save(function(err,data){
if(err) return done(err);
return done(null, data);

});
};

That code should work.

This is my final code. Can you tell me if I have any mistakes ?

var mongoose = require(‘mongoose’);
const mongoDB = require(‘mongodb’);
mongoose.connect(process.env.MONGO_URI,{ useNewUrlParser: true });
var Schema = mongoose.Schema;

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

var Person = mongoose.model(‘Person’, personModel);

var document = new Person({name: “John”,age: 24, favoriteFoods: [“Pizza”]});
const createAndSavePerson = function(done){
document.save(function(err,data){
if(err) return done(err);
done(null,data);
});

}
//----- DO NOT EDIT BELOW THIS LINE ----------------------------------
exports.PersonModel = Person;
exports.createAndSavePerson = createAndSavePerson;

Are you still using the same Glitch project? Does it have this new data?

Also, are you seeing any errors showing in the browser console?

I’m still using same project and I did not see any errors so far

OK, so I remixed your Glitch project and used my own MONGO_URI and everything passes just fine.

Are you sure you have the correct MONGO_URI in your .env file?

Make sure to surround it with single quotes like:

MONGO_URI='mongodb+srv://yourUsername:yourPassword@yourCluster.mongodb.net/databaseName?retryWrites=true&w=majority'

I only have MONGO_URI= in .env file. I don’t have a single quote.

@mehmetko95 Try changing:

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

to:

mongoose.connect(process.env.MONGO_URI, {useNewUrlParser: true}, function(error) {
  if (error) {
    console.log('problem connecting to db');
  }
});

and let me know if you see the “problem connecting to db” show in the Glitch console (not to be confused with the browser console) .

This tells me you have either the wrong MONGO_URI, or have not surrounded the MONGO_URI value with single quotes, or you have not properly configured MONGO Atlas to allow connection from any ip address. I suggest you go back through the introduction page for the MongoDB and Mongoose section and make sure you have followed all the instructions for configuring your MONGO Atlas database.

This code is the answer. In your .env file, replace with password you put.

var mongoose = require(‘mongoose’);
mongoose.connect(process.env.MONGO_URI,{ useNewUrlParser: true,useUnifiedTopology: true});
var Schema = mongoose.Schema;

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

var Person = mongoose.model(‘Person’, personSchema);

var createAndSavePerson = function(done) {

var document = new Person({
name: “Mehmet”,
age: 24,
favoriteFoods: [‘Pizza’]
});

document.save(function(err,data){
if(err) done(err);
done(null, data);
});

};
//----- DO NOT EDIT BELOW THIS LINE ----------------------------------

exports.PersonModel = Person;
exports.createAndSavePerson = createAndSavePerson;