2013-08-16 78 views
1

嘿所以我想让用户编辑他们的配置文件。但是,除非添加该字段,否则文档中并不总是存在一些字段。这是我目前如何调用查询,但如果该字段不存在,则会出现错误。如何让用户更新他们的个人资料?

下面是路由文件的代码:

User.findByIdAndUpdate(req.signedCookies.userid,{ 
       firstName: req.body.firstName.toLowerCase(), 
       lastName: req.body.lastName.toLowerCase(), 
       email: req.body.email.toLowerCase(), 
       firstNameTrue: req.body.firstName, 
       lastNameTrue: req.body.lastName, 
       emailTrue: req.body.email, 
       emailList: req.body.newEmail, 
       phone: req.body.phone, 
       phoneList: req.body.newphone, 
       socialAccounts: {socialAccount: req.body.socialAccount, socialAddress: req.body.socialAccountNew}, 
       currentCity: req.body.currentCity, 
       birthday: new Date(req.body.birthday) 
      }, function(err, user) { 
       console.log('here1'); 
       if(err) { 
        console.log("post2"); 
        console.log(err); 
        res.render('editUserProfileError', {title: 'Weblio'}); 
       } else { 
      console.log("post3"); 
        res.redirect('userProfile'); 
       } 
      }); 

}; 

我得到的错误是:

[TypeError: Cannot read property 'constructor' of undefined] 

我使用与的NodeJS猫鼬MongoDB的。我有猫鼬模式中的每个字段,但它们不保存在数据库中,除非它们是由用户手动添加的。

什么我都试过,但似乎看穿所有字段的值是一个很大的痛苦,循环,看看哪些存在,在一个阵列扔然后查询阵列

这里是一个解决方案,我试过,是不能正常工作,因为我需要的东西,让“:”通过......

var values = [ 
       firstNameVal = req.body.firstName.toLowerCase(), 
       lastNameVal = req.body.lastName.toLowerCase(), 
       emailVal = req.body.email.toLowerCase(), 
       firstNameTrueVal = req.body.firstName, 
       lastNameTrueVal = req.body.lastName, 
       emailTrueVal = req.body.email, 
       emailListVal = req.body.newEmail, 
       phoneVal = req.body.phone, 
       phoneListVal = req.body.newphone, 
       currentCityVal = req.body.currentCity, 
       birthdayVal = new Date(req.body.birthday) 
     ]; 

     var keyIcons = [ 
      firstName, 
      lastName, 
      email, 
      firstNameTrue, 
      lastNameTrue, 
      emailTrue, 
      emailList, 
      phone, 
      phoneList, 
      currentCity, 
      birthday 
     ]; 

     var existValues =[]; 
     for(var x = 0; x <keyIcons.length; x++) { 
      for(var i = 0; i < values.length; i++) { 
       if(values[i] === undefined) { 
        (console.log('undefined')) 
       } else { 
        existValues.push({keyIcons[i] : values[i]}); 
       } 
      }; 
     } 

     var socialAccountsVal = {socialAccount: req.body.socialAccount, socialAddress: req.body.socialAccountNew} 
     if(socialAccountsVal.socialAccount === undefined) { 

     } else { 
      existValues.push(socialAccounts); 
     }; 

另一种解决方案,我也许可以做的就是查询用户文档,然后看看什么样的价值观也有,但其实我困惑于如何去...

另外,我觉得谎言应该有一个更简单的方法来做到这一点?

编辑

这里是我的架构:

var mongoose = require('mongoose'), 
    Schema = mongoose.Schema, 
    ObjectId = mongoose.Schema.Types.ObjectId, 
    bcrypt = require('bcrypt-nodejs'), 
    SALT_WORK_FACTOR = 10; 



var UserSchema = new Schema({ 
    email: { type: String, required: true, lowercase:true, index: { unique: true } }, //might have to take off lowercase 
    emailTrue: { type: String}, 
    emailPrivate: {type: Boolean}, 
    emailList: {type: Array}, 
    password: { type: String, required: true }, 
    firstName: {type: String, lowercase:true, required: true, index: true}, 
    firstNameTrue: { type: String}, 
    lastName: {type: String, lowercase:true, required: true, index: true}, 
    lastNameTrue: { type: String}, 
    phone: {type: Number, required: true}, 
    phonePrivate: {type: Boolean}, 
    phoneList: {type: Array}, 
    birthday: {type: Date, required: true}, 
    birthdayPrivate: {type: Boolean}, 
    socialAccounts: {type: Array}, 
    currentCity: {type: String}, 
    date_created: {type: Date}, 
    email_confirmed: {type: Boolean}, 
    gender: {type: Number}, 
    currentDevice: {type: String}, 
    last_login: {type: Date} 
}, {collection: "users"}); 

module.exports = mongoose.model('User', UserSchema); 
+0

您的用户架构是什么样的? –

+0

我把它添加到底部 – Lion789

+0

看看这个[问题]的答案(http://stackoverflow.com/questions/16904932/mongoosejs-with-typeerror-cannot-read-property-constructor-of-undefined)有帮助。 –

回答

1

你可以先构建与领域中的一个对象在你req,然后将该对象传递给猫鼬方法:

var userFields = {}; 
if (req.body.firstName) userFields.firstName = req.body.firstName.toLowerCase(); 
if (req.body.lastName) userFields.lastName = req.body.lastName.toLowerCase(); 
...etc... 

User.findByAndUpdate(req.signedCookies.userid, userFields, function(err, user) { 
    ... 
}); 

这也允许您在将请求中的字段传递给数据库之前对其进行一些消毒处理。

+0

做这项工作,如果字段也是一个数组? – Lion789

+0

当字段是数组时,我通常会检查它是否有任何项目:'if(req.body.someArray && req.body.someArray.length> 0)userFields.someArray = req.body.someArray;' –

相关问题