【问题标题】:How to allow users to update their profile?如何允许用户更新他们的个人资料?
【发布时间】:2013-08-17 03:12:48
【问题描述】:

嘿,所以我正在尝试允许用户编辑他们的个人资料。但是,某些字段并不总是存在于文档中,除非他们添加了该字段。这是我当前调用查询的方式,但如果该字段不存在,则会出现错误。

这是路由文件的代码:

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 与 mongoose 一起用于 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);

【问题讨论】:

  • 您的用户架构是什么样的?
  • 我加到了底部
  • 看看这个question的答案是否有帮助。
  • 我认为这与我的解决方法不同......如果是我不确定我是否理解如何做到这一点,因为这些值总是随机的,有些包括在内,有些则不包括在内...

标签: node.js mongodb mongoose


【解决方案1】:

您可以先用req 中的字段构造一个对象,然后将该对象传递给mongoose 方法:

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) {
    ...
});

这还允许您在将请求中的字段传递到数据库之前对其进行一些清理。

【讨论】:

  • 当字段是数组时,我通常会检查它是否有任何项目:if (req.body.someArray &amp;&amp; req.body.someArray.length &gt; 0) userFields.someArray = req.body.someArray;
猜你喜欢
  • 1970-01-01
  • 2017-11-10
  • 2013-08-23
  • 2020-11-04
  • 2011-05-20
  • 2015-10-08
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多