使用Express和Mongoose将表单数据发布到现有MongoDB文档的数组

时间:2014-01-21 03:40:20

标签: javascript node.js mongodb express mongoose

我正在尝试创建一个Mongo文档,然后从表单更新文档以获得其他属性,其中一个属性包含一个对象数组。

我可以将除对象之外的所有内容保存到地址数组中。

以下代码段显示我当前尝试将对象保存到地址数组。我觉得我错过了我尝试过的推动或转移,似乎无法使语法正确。

Mongoose Schema:

var UserSchema = new mongoose.Schema({
     username: { type: String, lowercase: true }
    , password: { type: String }
    , email: { type: String, lowercase: true }
    , phone: { type: String }
    , newsletter: Boolean
    , created: { type: Date, default:   Date.now }
    , address: [{
        nickname: { type: String }
        , streetAddress: { type: String }
        , streetAddress2: { type: String }
        , state: { type: String }
        , zip: { type: String }
    }]    
});

模型方法: 首先,我创建一个帐户。表单只询问用户名,电子邮件,密码,然后重定向到玉器文件,用户可以填写表格的其余部分。

module.exports = exports = function(){
    //create account
    this.createAndSave = function (req, res ) {
        new User({
            username: req.body.username
            , password: req.body.password
            , email: req.body.email
            , phone: req.body.phone
            , address: [{
               nickname: req.body.nickname
               , streetAddress: req.body.streetAddress
               , streetAddress2: req.body.streetAddress2
               , state: req.body.state
               , zip: req.body.zip
            }]
        }).save(function (err, user){
            if (err) throw err;
            req.session.isLoggedIn = true;
            req.session.user = user.username;
            res.redirect('/account/' + user.username)
        })
    }

//update account
this.updateRequest = function (req, res) {
    User.update({username: req.user.username}, {
        username: req.body.username
        , email: req.body.email
        , phone: req.body.phone
        , newsletter: req.body.newsletter
        , address: [{
           nickname: req.body.nickname
           , streetAddress: req.body.streetAddress
           , streetAddress2: req.body.streetAddress2
           , state: req.body.state
           , zip: req.body.zip
        }]
      }, function (err) {
        res.redirect("/account/" + req.body.username);
    });
}

翡翠模板:(我相信这可能更干净)

h1 Edit User
#{user}
form(method="POST", action="/account/#{user.username}")
    input(type="hidden", name="_method", value="PUT")
    .form-group
        label(for="username") Name
        input#name.form-control(type="text", name="username", value= user.username )

    .form-group
        label(for="email") Email
        input#email.form-control(type="email", name="email", value= user.email )

    .form-group
        label Phone
        input#phone.form-control(type="text", name="phone", value= user.phone )

    .form-group
        label Newsletter Opt In/Out 
        input#newsletter(type="checkbox", name="newsletter", checked=(true===false ? "checked" : undefined))

    if(user.address.length > 0)
            for (var i = 0; i < user.shippingAddresses.length; i++) {}>)
                .form-group
                    label Street Address
                    input#address.form-control(type="text", name="streetAddress", value= user.shippingAddresses[i].streetAddress )

                .form-group
                    label Address Continued
                    input#address2.form-control(type="text", name="streetAddress2", value= user.shippingAddresses[i].streetAddress2 )

                .form-group
                    label Zip Code
                    input#zip.form-control(type="text", name="zip", value= user.shippingAddresses[i].zip )
    else
                .form-group
                    label Location Nick Name
                    input#address.form-control(type="text", name="nickname", value= )

                .form-group
                    label Street Address
                    input#address.form-control(type="text", name="streetAddress", value= )

                .form-group
                    label Address Cont.
                    input#address2.form-control(type="text", name="streetAddress2", value= )
                .form-group
                    label State
                    input#state.form-control(type="text", name="state", value= )
                .form-group
                    label Zip Code
                    input#zip.form-control(type="text", name="zip", value= )


    button(type="submit") Update Account

另外还有另一个地址,这就是地址是数组的原因。

任何方向都会非常有用,因为我随时都可能会精神错乱。如果您有任何进一步的代码,请告诉我。

还有其他需要注意的地方,我无法从更新功能获取任何更新数据以保存到mongo。

谢谢!

1 个答案:

答案 0 :(得分:3)

这是我提出的解决方案。我发现要更新的文档并将对象推送到存储数组的属性。

示例方法:

this.addAddress = function (req, res) {
    var newAddress = {
           nickname: req.body.nickname,
           streetAddress: req.body.streetAddress,
           streetAddress2: req.body.streetAddress2,
           state: req.body.state,
           zip: req.body.zip
    }
    User.update({username: req.session.user}, { $push : {
            address: newAddress
        }}, {upsert: true}, function ( err ) {
                if(err){
                        console.log(err);
                }else{
                        console.log("Successfully added");
                }
        })
}