TypeError: Cannot use 'in' operator to search for '_id' in male

后端 未结 1 1341
北恋
北恋 2021-02-03 11:11

Hi i am having issue with using the new versions of node.js Earlier i used a code like this

 label(for=\'user_sex\') Sex:
 select(id=\'user_sex\', name=\'user[se         


        
1条回答
  •  清酒与你
    2021-02-03 12:18

    You are getting this error because you're not constructing your model instance properly. It expects a hash of properties and their corresponding values but the parameter you're are providing is a string instead. From your code above, req.body.user is a hash {sex: "male"} whereas req.body.user.sex is just a string "male". You can do;

    user = new User({sex: "male"});
    

    But you can't do;

    user = new User("male");
    

    That explains why your first "User" instance with req.body.user parameter works but fails with req.body.user.sex parameter. I'm not sure yet what you're trying to achieve with var sex = new User(req.body.user.sex); Do you want to create another User model instance? or an associated sex model?

    0 讨论(0)
提交回复
热议问题