【问题标题】:How to avoid setting fields to empty strings if value exists如果值存在,如何避免将字段设置为空字符串
【发布时间】:2019-06-07 12:22:57
【问题描述】:

我正在开发我的第一个 node.js 应用程序,我需要一些帮助。

我使用 MongoDb 作为数据库。

在我的应用程序中,我创建了一个(注册)方法,它读取用户输入(例如电子邮件和密码)并设置其他字段,例如 first-namelast-name 清空字符串。

exports.postSignup = (request, response, next) => {
    const email = request.body.email;
    const password = request.body.password;
    const fonfirmPassword = request.body.confirmPassword;
    User.findOne({ email: email })
        .then(userDoc => {
            if (userDoc) {
                request.flash('error', 'Email already exists, please pick another!')
                return response.redirect('/auth/signup');
            }
            return bcrypt.hash(password, 12)
                .then(hashedPassword => {
                    const user = new User({
                        firstName: '',
                        lastName: '',
                        email: email,
                        photoUrl: '',
                        password: hashedPassword,
                        cart: { items: [] }
                    })
                    return user.save();
                })
                .then(result => {
                    response.redirect('/auth/login');
                    const signup = {
                        to: email,
                        from: 'support@company.com',
                        templateId: keys.SIGNUP_TEMPLATE_ID,
                        dynamic_template_data: {
                            subject: 'Signup succeeded successfully!',
                        },
                    };
                    sgMail.send(signup);
                })
                .catch(err => {
                    console.log(err);
                });
        })
        .catch(err => {
            console.log(err);
        });
};

上面的代码运行良好... 用户登录到他们的帐户后,该用户可以导航到他们的个人资料页面并设置他们的 first-namelast-name,如附件所示图片。

enter image description here

所以我创建了另一种方法,允许用户设置他们的 first-namelast-namephoto-Url

exports.postAddProfile = (request, response, next) => {
    const firstName = request.body.firstName;
    const lastName = request.body.lastName;
    const photoUrl = request.body.photoUrl;

    User.findOne({ userId: request.user.userId })
        .then(user => {
            user.firstName = firstName;
            user.lastName = lastName;
            user.photoUrl = photoUrl;
            return user.save();
        })
        .then(result => {
            console.log('Added Profile Info');
            response.redirect('/');
        })
        .catch(err => {
            console.log(err)
        });
};

此代码也可以正常工作但问题是如果用户设置他们的 first-namelast-namephoto-Url第一次喜欢(Jonas、Jsk 和 https://photourl.com) 然后第二次,如果用户只更改了 first-name,则 last-namephoto-Url 再次设置为空字符串。

我怎样才能避免这种情况?

【问题讨论】:

    标签: javascript html node.js mongodb ejs


    【解决方案1】:

    空字符串在 JS 中是虚假的,所以只需检查 response 的值是否不是空字符串:

    User.findOne({ userId: request.user.userId })
        .then(user => {
            user.firstName = firstName ? firstName : user.firstName;
            user.lastName = lastName ? lastName : user.lastName;
            user.photoUrl = photoUrl ? photoUrl : user.photoUrl;
            return user.save();
        })
    

    【讨论】:

    • 很高兴为您服务:)
    • 我需要其他问题的帮助 似乎User.findOne({ userId: request.user.userId }) 没有获取当前登录的用户。因此,如果我的数据库中有多个单独的用户 - 更改总是会影响数据库中的第一个用户。
    • 我认为问题是因为没有request.user引起的。您的解决方案是使用 request.params.userId 如果您希望用户 id 在 url 中可见(路由的链接将是 yourwebsite.com/user/:userId [冒号是强制性的])或者只是使用 request.body 再次与 id在那里。 或者:你可以为你的网站实现浏览器会话,不错的教程here
    • 我花了一些时间,但最终修复了 - 再次感谢
    猜你喜欢
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-05
    • 2023-03-27
    • 2020-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多