我正在尝试对我的应用上的用户信息进行基本更新。
我的用户Schema在其自己的模型文件中定义:
var UserSchema = new mongoose.Schema({
firstname: String,
lastname: String,
username: String,
password: String,
email: String,
zip: String,
bio: String,
influences: String,
favBooks: String,
notWriting: String,
favHero: String,
favVillain: String
});
我的用户路径js文件中正在导入和需要用户模型。
var User = require('../models/userModel');
当用户创建帐户时,只需要名字,姓氏,电子邮件和密码。其余的字段不是。
当用户点击按钮编辑他们的个人资料时,这是我编辑“show”表单的路线。
// edit profile get route
router.get('/:id/edit', isLoggedIn, function(req, res) {
// find the right user by id
User.findById(req.params.id, function(err, foundUser) {
if (err) throw err
res.render('editProfile', {currentUser: foundUser});
});
});
返回正确的(当前)用户。
在editprofile html / view(我使用pug作为模板引擎,所以看起来很奇怪),我有这个表单。
form(action='/users/' + currentUser._id + '?_method=PUT', method='POST')
.form-row
.form-group
label(for="profilePic") Add or change your profile picture
input.form-control-file#profilePic(type="file", name='profilePic')
.form-row.mt-2
.col-sm-6
label(for="firstname") First name
input.form-control#firstname(type="text", name='firstname')
.col-sm-6
label(for="lastname") Last name
input.form-control#lastname(type="text", name='lastname')
.form-row.mt-2
.col-sm-6
label(for="country") Country/Region
select.form-control#country
option United States
.col-sm-6
label(for="zip") Zip Code
input.form-control(type='text', name='zip', placeholder='Zip')
.form-row.mt-2
.col-sm-12
label(for="bio") Bio/Summary
textarea.form-control#bio(name="bio", cols="30", rows="10", placeholder="Tell us about you...")
.form-row.mt-2
label(for="influences") Who are your influences?
small.ml-2 (comma separated values only)
input.form-control#influences(type='text', name='influences', placeholder='i.e. JK Rowling, Charles Dickens, Sylvia Plath, Stephen King')
.form-row.mt-2
label(for="favBooks") What are your favorite books?
small.ml-2 (comma separated values only)
input.form-control#favBooks(type='text', name='favBooks', placeholder='i.e. Ulysses, War and Peace, Emma, The Great Gatsby')
.form-row.mt-2
label(for="notWriting") When I'm not writing, I'm...
small.ml-2 (comma separated values only)
input.form-control#notWriting(type='text', name='notWriting', placeholder='i.e. reading, eating, sleeping')
.form-row.mt-2
label(for="favHero") My favorite hero/heroines are...
small.ml-2 (comma separated values only)
input.form-control#favHero(type='text', name='favHero', placeholder='i.e. Harry Potter, robinson Crusoe, Sherlock Holmes, Guinevere')
.form-row.mt-2
label(for="favVillain") My favorite villains are...
small.ml-2 (comma separated values only)
input.form-control#favVillain(type='text', name='favVillain', placeholder='i.e. Professor Moriarty, Sauron, Dracula, The White Witch')
.form-row.mt-2
.btn-group.ml-auto
a.btn.btn-danger.mr-2.text-white Discard changes
button.btn.btn-primary(type='submit') Save profile changes
表单正在捕获数据,因为我能够使用此put / post代码在服务器端的用户路由文件中控制记录值。
router.put('/:id', isLoggedIn, function(req, res) {
// find and update the correct user
var firstname = req.body.firstname;
var lastname = req.body.lastname;
var zip = req.body.zip;
var bio = req.body.bio;
var influences = req.body.influences;
var favBooks = req.body.favBooks;
var notWriting = req.body.notWriting;
var favHero = req.body.favHero;
var favVillain = req.body.favVillain;
});
我尝试了两种不同的方法来使用新信息更新用户,但两种方法都不起作用。不知道我做错了什么。正在找到正确的用户,它没有控制台记录任何错误,但在我运行代码并返回到mongo db并找到用户之后,旧信息仍然存在。有什么想法吗?
return User.update(req.params.id, {firstname: firstname});
User.findByIdAndUpdate(req.params.id, firstname, function(err, foundUser) {
if(err) {
console.log(err);
} else {
console.log("Updated")
console.log(foundUser);
}
});
// redirect to user profile page
答案 0 :(得分:0)
您需要传入一个对象作为第二个参数而不是findByIdAndUpdate
调用中的字符串。
使用
User.findByIdAndUpdate(req.params.id, { firstname }, function(err, foundUser) {
而不是
User.findByIdAndUpdate(req.params.id, firstname, function(err, foundUser) {