Search code examples
mongodbmongoosemongoose-schema

MongoDB not recognizing field given in Schema


Schema-

let userSchema = new mongoose.Schema({
  count : Number,
  username : {type: String, required: true},
  log:[
    {description : String,
    duration : Number,
    date : Date}]
    
});

But when im saving it like this-

let newUser = new fTrack(request.body)
          let newUserDetails={};
          newUserDetails['username']=inputUsername
          newUserDetails['count']=0
          console.log(newUserDetails)
          newUser.save(newUserDetails,(err,result)=>{
            // console.log(result)
            response.json({username:result.username,_id:result.id})
            return;
          })

The mongodb entry doesn't show it

I've searched for a solution to this but haven't found anything so far


Solution

  • Found the answer, was arbitrarily using request.body, in

    let newUser = new fTrack(request.body)
    

    there should be empty, or there should be some other object in it. Also, the newUserDetails in

    newUser.save(newUserDetails,(err,result)=>{
    

    is doing nothing.