TypeError:无法使用'in'运算符在男性中搜索'_id' [英] TypeError: Cannot use 'in' operator to search for '_id' in male

查看:173
本文介绍了TypeError:无法使用'in'运算符在男性中搜索'_id'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用新版本的node.js有问题
以前我用了这样的代码

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[sex]')
   option(id='user_male',value=user.sex,selected=1)='male'
   option(id='user_female',value=user.sex)='female'

和app.js中的代码

And code in app.js

 var user = new User(req.body.user);
 -- other code
 var sex = new User(req.body.user.sex);
 User.find({}, function(err, users) {
 for(var i = 0;i< users.length;i++) {
    if(users[i].email == email) {
       useremail = users[i].email;
    }
 }
 if(!useremail) {
    user.save(function(err) {
      if (err) return userSaveFailed();
      req.flash('info', 'Your account has been created');
      emails.sendWelcome(user);

      switch (req.params.format) {
        case 'json':
          res.send(user.toObject());
        break;

        default:
          req.session.user_id = user.id;
          res.redirect('/userinfo');
      }
    });
 }

完整的错误日志如下:

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

at model.Document.$__buildDoc (C:\SocialNetwork\node_modules\mongoose\lib\document.js:159:27)
at model.Document (C:\SocialNetwork\node_modules\mongoose\lib\document.js:58:20)
at model.Model (C:\SocialNetwork\node_modules\mongoose\lib\model.js:38:12)
at new model (C:\SocialNetwork\node_modules\mongoose\lib\model.js:2092:11)
at C:\SocialNetwork\app.js:1033:13
at callbacks (C:\SocialNetwork\node_modules\express\lib\router\index.js:272:11)
at param (C:\SocialNetwork\node_modules\express\lib\router\index.js:246:11)
at param (C:\SocialNetwork\node_modules\express\lib\router\index.js:243:11)
at pass (C:\SocialNetwork\node_modules\express\lib\router\index.js:253:5)
at Router._dispatch (C:\SocialNetwork\node_modules\express\lib\router\index.js:280:5)

问题出现在connect-fo上我知道的rm现在已经弃用了,所以我现在使用强大的。任何人都可以帮我解决这个错误

The problem seems with connect-form which i know is deprecated now, so i am using formidable now. Can anybody help me out to solve this error

推荐答案

你正在得到这个错误,因为你没有正确构建你的模型实例。它期望属性的哈希值及其对应的值,但是您提供的参数是一个字符串。
从上面的代码,req.body.user是一个哈希 {sex:male} 而req.body.user.sex只是一个字符串男。你可以做;

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"});

但你不能做;

user = new User("male");

这就解释了你的第一个用户实例与req.body.user参数的作用,但是与req失败.body.user.sex参数。
我不确定你正在尝试使用 var sex = new User(req.body.user.sex); 你想要创建另一个用户模型实例?或相关的性别模型?

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?

这篇关于TypeError:无法使用'in'运算符在男性中搜索'_id'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆