MongoDB challenge passed on FCC but no entry in Database?

MongoDB challenge passed on FCC but no entry in Database?
0

Hi all,

I was able to pass the “MongoDB and Mongoose - Create and Save a Record of a Model” challenge, which was supposed to result in creating and saving a document in my database. When looking at my database, it did not show up. Here is my code:

var createAndSavePerson = function(done) {
  
var person = new Person ({name: 'John', age: 20, favoriteFoods: ['burgers', 'pizza']});

  person.save(function(err, data) {
      if(err) return done(err);
    done(null, data)[/;
  })
};

One thing I noticed is if I typed within my app.js file the following:

createAndSavePerson();

My new person gets added to the database. However, the log throws the following error:
TypeError: done is not a function

Can someone please help me understand what I’m doing wrong?

Second question:
I’ve gotten to the URL Shortener project and I was wondering if there’s something you need to add to the app.js file in terms of routing to the appropriate database collection. I ask because it seems that the FCC MongoDB challenges does this for you (and I can’t figure out how to add anything to my projects collection).

Thank you,

First of all, a typo here:

done(null, data)[/;

First question:

  • You write the function (gj), there is no record in your database because you never call it.
  • As you said, when you call it the record gets added (the method save executes successfully) but right after the program crashes: done is not a function is actually true, you didn’t provide any callback! :stuck_out_tongue:
    Try createAndSavePerson((err, data ) =>{ if(err){console.log(err);} console.log(data);}

Second question:

  • When you write a Model in mongoose you can specify the collection it refers to, otherwise it creates a collection named as the plural of your Model name^^
When you call mongoose.model() on a schema, Mongoose compiles a model for you.

var schema = new mongoose.Schema({ name: 'string', size: 'string' });
var Tank = mongoose.model('Tank', schema);

The first argument is the singular name of the collection your model is for. Mongoose automatically looks for the plural, lowercased version of your model name. Thus, for the example above, the model Tank is for the tanks collection in the database.

From mongoose doc (link to mongoose Doc)

1 Like

Hey there!

In terms of the typo, that was me trying to put a spoiler tag on the post and then failing at fully removing it. Sorry about that!

For the first issue, I’m not sure how I missed that within the documentation! Regardless, I’ve tried the code you suggested and it worked :slight_smile:

In terms of the second question, that makes sense now! That’s why in the Mongoose challenges the ‘people’ collection was created by itself. Amidst all of the Googling I had done, I read something about plural versions of words being created for collections but I wasn’t quite sure what it meant or how that worked. I appreciate you explaining this! I’ll have to read the documentation more closely next time.

Thank you for taking the time to answer my questions!! :smiley:

1 Like