Create and save a person gives 'item.name' must be a string error

I’ve looked through several similar issues with the third MongoDB and mongoose challenge for FreeCodeCamp’s backend certification, but even when I try using other people’s glitch.com project links, I get the same ‘item.name’ must be a string error.

As it is, everytime I link to my project page and attempt to complete the challenge it says “item.name” should be a String.

I’ve tried re-writing my code several times, and have even looked up code that other people say passes the challenge, but the same error shows.

const createAndSavePerson = function(done) {
  
  console.log('connection state to create and save:', mongoose.connection.readyState);
  
  const Kurt = new Person(
    {
      name: "Kurt",
      age: 32,
      favoriteFoods: [ 'Cheese', 'Bread' ]
    }
  )

  Kurt.save( (err, data, done) => {
    if (err) {
      console.log(err)
      return done(err)
    }
    console.log(`${data} saved to collection`)
    return done(null, data)

  })
  
}

Link to my code here

Thanks!

  1. Trying using type: [String] for the array.
    https://mongoosejs.com/docs/schematypes.html#arrays

  2. You have createAndSavePerson twice, remove the inner createAndSavePerson function definition.

 var createAndSavePerson = function(done) {
  var Person = mongoose.model('Person', personSchema);

    var p = new Person;
    p.name = "John";
    p.age = 18;
    p.favoriteFoods = ["hotpot", "suantangyu"];
	// Remove this function
    var createAndSavePerson = function(done){

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

I’m not sure, just an idea: maybe the way the person is created is important:

// object as argument
  let person = new Person({
    name: "John",
    age: 42,
    favoriteFoods:["Fish", "Chips"]
  });

I tried that, am getting same error. Also, in the console I’m noticing it says “done is not a function”.

Thanks! I updated the code, but am still getting the same error.

What link are you pasting into the ‘solution’ field for the fCC challenge?
I tried this link first just to see if I would get the same error (“item.name” should be a string)
https://glitch.com/edit/#!/inky-cabbage?path=myApp.js:107:22

and, indeed, I got that error. The link you should be using can by gotten by clicking ‘show’ at the top of your Glitch editor page, opening a new window, and copying the url from the address bar.
https://inky-cabbage.glitch.me/ is what I got when I did so. Pasting this into the ‘solution’ field eliminated the previous error, but introduced a new one about not being able to create and save a db item.

The callback for the save() method in Mongoose takes two parameters, err and doc. doc in your case is called data. So, look at the parameters you’re passing into your save method’s callback and adjust accordingly.

https://mongoosejs.com/docs/api/document.html#document_Document-save

You know, it’s funny. On some of the FCC mongo challenges, the ‘project page’ link on glitch works, but the ‘live code’ link works on others!

It’s working now. I’m using the ‘live app’ link from glitch.

const createAndSavePerson = function(done) {
  
  console.log('connection state to create and save:', mongoose.connection.readyState);
  
  const Kurt = new Person(
    {
      name: "Kurt",
      age: 32,
      favoriteFoods: [ 'Cheese', 'Bread' ]
    }
  )

  Kurt.save((err, data) => {
    if (err) return done(err);
    return done(null,data);
  })
}

I had thought that ‘done’ was a special method for mongoose, but this code also works, and shows that the names ‘err’, ‘docs’, ‘data’, ‘done’, don’t matter so long as they are consistent.

const createAndSavePerson = function(turkeySandwich) {
  
  console.log('connection state to create and save:', mongoose.connection.readyState);
  
  const Kurt = new Person(
    {
      name: "Kurt",
      age: 32,
      favoriteFoods: [ 'Cheese', 'Bread' ]
    }
  )

  Kurt.save( (err, anyNameWillDo) => {
    if (err) return turkeySandwich(err);
    return turkeySandwich(null, anyNameWillDo);
  })
}