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

hi mr this are code please i need your help


const Schema = mongoose.Schema;
const personSchema = new Schema({
  name:{type: "String", required: true},
  age: "number",
favoriteFoods: ["string"]

  });

const Person = mongoose.model("Person", personSchema);
const createAndSavePerson = (done) => {
  const person = new Person({
    name: "fabiola",
    age: 18,
    favoriteFoods: ["riz", "Fruits"]
  });
    person.save(function(err, data) {
    return console.log(err)
    done(null, data);
  });

}

they takl me to creat and save a db item should succes
//

see the link //

When you need help with these challenges could you post a link to your full code please and also a link to the challenge?
Your error is in this line:

return console.log(err)

You are terminating the function even if there is no error, so saving the db record will fail. You should only return an error if there is an error to return. So, turn this into a conditional statement.

https://replit.com/@bowak/boilerplate-mongomongoose#myApp.js)
https://www.freecodecamp.org/learn/back-end-development-and-apis/mongodb-and-mongoose/create-and-save-a-record-of-a-model

look its not always working

const Person = mongoose.model("Person", personSchema);
const createAndSavePerson = (done) => {
  const person = new Person({
    name: "fabiola",
    age: 18,
    favoriteFoods: ["riz", "Fruits"]
  });
  person.save(function(err, data) {
    if (err) return console.log(err);
    done(null, data)
  });
};

Your code works for me. Are you getting errors in the console?

this is what is showing on my console

      'ac-2lvmfyc-shard-00-02.guig8mn.mongodb.net:27017' => ServerDescription {
        address: 'ac-2lvmfyc-shard-00-02.guig8mn.mongodb.net:27017',
        error: MongoNetworkError: connection <monitor> to 18.207.36.39:27017 closed
            at Connection.handleIssue (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:131:15)
            at TLSSocket.<anonymous> (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:64:35)
            at TLSSocket.emit (node:events:513:28)
            at node:net:313:12
            at TCP.done (node:_tls_wrap:587:7),
        roundTripTime: -1,
        lastUpdateTime: 1380365,
        lastWriteDate: null,
        opTime: null,
        type: 'Unknown',
        topologyVersion: undefined,
        minWireVersion: 0,
        maxWireVersion: 0,
        hosts: [],
        passives: [],
        arbiters: [],
        tags: []
      }
    },
    stale: false,
    compatible: true,
    compatibilityError: null,
    logicalSessionTimeoutMinutes: null,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,

Do you have your MONGO_URI stored in your Secrets tab?

yes sir if I didn’t I couldn’t reach the chalinge though

Your repl passes for me with my MONGO_URI. It should pass for you too I think, if you have set your MONGO_URI correctly.

I don’t know but I did everything but it doesn’t work

so now i don’t know what to do

If you go to your Secrets tab, you have a MONGO_URI variable which looks something like this? (I have redacted my username and password but that’s where you should see your username and password).

I have to change my username and password? because I've been using it since the first chalange

No, you shouldn’t need to change anything. I was just checking if you have correctly set your MONGO_URI variable in your Secrets tab? I only ask because your repl passes the challenge for me, so there’s nothing wrong with your code.

so what should i do to pass this chanlange. I must continue

I don’t know what to tell you. I have forked your repl and used my MONGO_URI and it passes immediately for me. I can only assume that there’s a problem with your MONGO_URI. Perhaps your username/password isn’t correct? Or there’s some other problem with the format of your URI?

For instance, if your username/password contains certain characters, you need to use percent encoding: https://www.mongodb.com/docs/manual/reference/connection-string/

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