MongoDB and Mongoose - Create and Save a Record of a Model, Creating and saving a db item should succeed

Please help I can’t find the solution how to pass this challenge
here’s my code

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 createAndSavePerson = function(done) {
  var dingusMan = new Person({name: "Dingus", age: 8, favoriteFoods: ["beef", "cheetos"]});
  
  dingusMan.save(function(err, data) {
    if (err) return console.error(err);
    done(null, data)
  });

};

I have this error
Creating and saving a db item should succeed

Thanks in advance

1 Like

Welcome, jhaycawasa.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

To help your question: If you have run the script to create a model, and it was not 100% correct to begin with, then your database has an incorrect collection, and you could try:

  1. Going into your Mongo account, and clearing all records.
  2. Re-running the tests/script.

Or

  1. Go back to the first challenge, and concurrently complete all of them again.

Hope this helps

Just wanted to update this, I was struggling with this and this was good advice! But it took me a bit of research to clear all records.

When you start with Mongo, as is not specified in the challenges, you have to create a collection. I created one (database name “dbtest” and collection name also “dbtest”) and then you have to change the bit in your URI to whatever you set it to. Was having trouble just because there was no collection for Mongo to access. Hope this helps, this challenge took me forever to pass

1 Like

Hey there,

Good point. This could do with mentioning during the first lesson, in case Campers get stuck.

This is not quite true, because the default URI contains 'test' (mentioned here). Also, using Mongoose (as this challenge does) automatically creates a database (typically with the same name as the collection).

Ah! Also, I didn’t end up fixing it lol, about to post again.

but yes, thank you for responding! On me for not seeing the default URL bit.

Also, seems like fCC has migrated to Glitch? All of the challenges in this section cite Repl.it but all of the responses on the forum suggest using Glitch. Might be useful to change some of those (I was thinking of sending in PRs over the next few days for what it’s worth, just have been busy!)

The opposite, actually. Around August, 2020, we migrated from Glitch to Repl.it.


If you can think of a decent way to make Campers aware of the possible need to delete documents in the database, then a PR would be welcome.

If you have not recently, remember to read the Contributors Docs.

Will read the contributor docs! Have seen a few things I wanted to tweak but have not gotten around to setting up an environment/actually tweaking things.

Was mainly thinking something like “NOTE: We were previously encouraging campers to use Glitch instead of Repl.it, so posts on the freeCodeCamp forum may refer to Glitch. Most of this advice is applicable to projects on Repl.it as well.” or something like that but perhaps more eloquent.

where do I find the records in my Mongo DB
Actually, I passed the test (my error was not putting quotes around the string items in the array), but I double checked the cluster in my Mongo DB and could find no data there