【问题标题】:PUSH id in order like add to cart按添加到购物车的顺序推送 id
【发布时间】:2022-06-17 17:42:23
【问题描述】:

我使用简单的 CRUD 为用户和产品创建了模型架构,我的下一个项目是我的模型架构顺序,我将我的 userId 和 projectId 按顺序推送到数组中。

这是我在控制器中创建的代码

module.exports.makeOrders = (reqBody) => {

let newOrder = new Order({
    totalAmount : reqBody.totalAmount,
    usersOrder.push({
        userId : reqBody.userId,
        project : reqBody.projectId
    }),
})

return newOrder.save().then((order, error) =>{
    if(error){
        return false;
    }
    else{ 
        return true;
    }
})
}

这是我的路线

router.post("/checkout", (req, res) => {
let data = {
    userId : req.body.userId,
    productId : req.body.productId
}
userController.makeOrders(data).then(resultFromController => res.send(resultFromController))
})

这是我的模特

const orderSchema = new mongoose.Schema({
totalAmount : {
    type : Number,
    required : true
},
purchasedOn : {
    type : Date,
    default : new Date
},
usersOrder :[
                {
                    userId : {
                        type : String,
                        required : true
                },

                    productId : {
                        type : String,
                        required : true
                },
            }
        ]
    })

这是我在邮递员中输入的内容

{
"totalAmount" : 1000,
"userId" : "62a9c46c4d15dc8157c375aa",
"productId" : "62aafe01d9337ce87ff5aaa1"
}

我遇到的错误是“SyntaxError: Unexpected token '。” " 根据我所知道的,我将 push 方法放在了错误的位置。我只是在正在工作的用户中复制 create 方法。我不知道为什么它不能在订单控制器中工作。 笔记。我刚开始学习json。

【问题讨论】:

    标签: arrays json mongodb express mongoose


    【解决方案1】:
    let newOrder = new Order({
        totalAmount : reqBody.totalAmount,
        usersOrder: newOrder.usersOrder.push({
            userId : reqBody.userId,
            project : reqBody.projectId
        }),
    })
    

    这样试试

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多