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

时间:2023-01-18 07:53:17

I'm attempting to create a Mongo document then update the document form a form to have additional properties, one of which has an array of objects.

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

I'm able to save everything except objects to the address array.

我能够将除对象之外的所有东西保存到地址数组中。

The following code snippets show my current attempt to save an object to the address array. I feel like I'm missing a push or shift which I've tried and can't seem to get syntax correct.

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

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 }
    }]    
});

Model Methods: First I create an account. The form only asks for username, email, password then redirects to the jade file where users can fill out the rest of the form.

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

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

Jade Template: (I'm sure this could be cleaner)

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

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

Additionally there is another address only form which is why the address is an array.

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

Any direction would be very helpful as I may go unhinged at any moment. If you any further code let me know.

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

Something else to note, I'm not able to get any of the updated data from the update function to save to mongo.

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

Thanks!

谢谢!

1 个解决方案

#1


3  

Here is the solution I came up with. I find the document to update and push an object to the property that stores the array.

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

Example method:

示例方法:

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");
                }
        })
}

#1


3  

Here is the solution I came up with. I find the document to update and push an object to the property that stores the array.

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

Example method:

示例方法:

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");
                }
        })
}