在编辑页面时,在Mongo中复制字段,只有一个字段

时间:2017-01-14 05:37:16

标签: node.js mongodb

我有一个更新用户信息的页面,当我在页面上编辑其他任何内容时,它编辑得很好并且显示正确。然而,在城市部分,它只是附加到该领域而不是更新它。所以我得到了很多城市,只有一个!

前端表格:

<form action ="/user/<%= user._id %>/?_method=PUT" method="POST">

<section class="side_nav_content">

                <section id="name_and_address_section" class="col-md-4">
                    <div class="form-group">
                        <label for="username">Username</label>
                        <input class="form-control" id="username" type="text" name="user[username]" placeholder="Username" value ="<%= user.username %>">
                    </div>
                    <div class="form-group">
                        <label id="nickname">Nickname</label>
                        <input class="form-control" id="nickname" type="text" name="user[profile.nickname]" placeholder="Nickname" value="<%= user.profile.nickname %>">
                    </div>
                    <div class="form-group">
                        <label id="email">Email</label>
                        <input class="form-control" id="email" type="email" name="user[email]" placeholder="Email@wherever.com" value="<%= user.email %>">
                    </div>
                    <div class="form-group">
                        <label id="first">First Name</label>
                        <input class="form-control" id="first" type="text" name="user[name][first]" placeholder="First Name" value="<%= user.name.first %>">
                    </div>
                    <div class="form-group">
                        <label id="middle">Middle Name</label>
                        <input class="form-control" id="middle" type="text" name="user[name][middle]" placeholder="Middle Name" value="<%= user.name.middle %>" >
                    </div>
                    <div class="form-group">
                        <label id="last">Last Name</label>
                        <input class="form-control" type="text" name="user[name][last]" placeholder="Last Name" value="<%= user.name.last %>">
                    </div>
                    <div class="form-group">
                        <label id="address">Street Address</label>
                        <input class="form-control" id="address" type="text" name="user[address]" placeholder="Street Address" value="<%= user.address %>">
                    </div>
                    <div class="form-group">
                        <label id="city">City</label>
                        <input class="form-control" id="city" type="text" name="user[city]" placeholder="City" value="<%= user.city %>">
                    </div>
                    <div class="form-group">
                        <label id="zip">Zip Code</label>
                        <input class="form-control" id="zip" type="number" name="user[zipCode]" placeholder="Zip Code" value="<%= user.zipCode %>">
                    </div>
                    <button class="btn btn-lg btn-default btn-primary">Save</button>
                    <a href="/dashboard" class="btn btn-lg btn-info">Cancel</a>
                </section>

这是路线:

router.put("/user/:id", isLoggedIn, function(req,res){
console.log(req.user.username);
User.findByIdAndUpdate(req.user.id,
    {$set: req.body.user},
    {save: true, upsert: true, runValidators: true, new: true}, function(err, updatedUser) {
        if (err) {
            console.log(err);
        } else {
            console.log(updatedUser.username);
            res.render("admin", {user: updatedUser});
        }
    })
});

以下是用户架构:

const userSchema = new mongoose.Schema({
username: {type:String, required: true, unique: true},
email: {type: String, required: false, unique: false},
password: { type: String, required: true},
resetPasswordToken: String,
resetPasswordExpires: Date,
profile: {picture: String, nickname: String},
permission:{app: Number}, //This will tell the app what level, 0 for Admin, 1 for User
name: {
    first:String,
    middle:String,
    last:String,
    other: {
        startDate: Date,
        endDate: Date,
        first: String,
        middle: String,
        last: String
    }
},
lastName: String,
address: String,
city: String,
state: String,
zipCode: Number,
phone: Number,
birthday: Date,
birthplace: String,
userCover: String,
categories: Array,
defaultPermission: Number,
events: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: "Event"
}],
moments: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: "Moments"
}],
instants: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: "Instant"
}],
groups: [
    {family:{
    type: mongoose.Schema.Types.ObjectId,
    ref: "User"}
    },
     {friends:{
        type: mongoose.Schema.Types.ObjectId,
        ref: "User"
    }
    },
    {colleagues:{
        type: mongoose.Schema.Types.ObjectId,
        ref: "User"
    }
    }],
likedMoments: [{ //This is for what this person likes, so they can refer later
    type: mongoose.Schema.Types.ObjectId,
    ref: "Moments"
}],
likedEvents: [{  //This is for what this person likes, so they can refer later
    type: mongoose.Schema.Types.ObjectId,
    ref: "Event"
}],
favoriteEvents: [{ //This is for personal events that are favorited
type: mongoose.Schema.Types.ObjectId,
ref: "Event"
}],
favoriteMoments: [{  //This is for personal moments that are favorited
    type: mongoose.Schema.Types.ObjectId,
    ref: "Moments"
}]
})

数据只会添加到city但是它刚刚更新的任何其他字段,我不知道该怎么办,因为我不会对城市有任何不同。正如您所见,我只是一次更新整个文档?

1 个答案:

答案 0 :(得分:0)

我在页面的另一部分输入了一个输入user[city]被引用并再次保存。所以它正在那里填充,当保存时它保存了第一个字段的旧值和新值!

也许这将有助于将来遇到类似问题的人。