类型错误:无法读取控制器上未定义的属性“forEach”

时间:2021-02-10 05:48:27

标签: mern

这是我的代码,我试图将我的内容从 rawmaterialSchema 推送到 UserSchema 我不知道哪里出错了,它说无法读取 forEach 的属性

exports.pushMaterialsInList = (req, res, next) => {
    let rawmaterials = []
    console.log(req.body.rawmaterial)
    req.body.rawmaterial.forEach(material => {
        // console.log(req.body.materials)
        rawmaterials.push({
            _id: material._id,
            name: material.name,
            size: material.size,
            partyname: material.partyname,
            partyphonenumber: material.partyphonenumber,
        })

    })

    User.findOneAndUpdate({ id: req.profile._id }, { $push: { rawmaterials: rawmaterials } }, { new: true },
        (err, materials) => {
            if (err) {
                return res.status(400).json({
                    error: " Unable to save material List"
                })
            }
            next();
        }
    )
};


我收到此类型错误

TypeError: Cannot read property 'forEach' of undefined
    at exports.pushMaterialsInList (C:\Users\lmtla\OneDrive\Desktop\praveenbhai\backend\controllers\user.js:42:26)
    at Layer.handle [as handle_request] (C:\Users\lmtla\OneDrive\Desktop\praveenbhai\backend\node_modules\express\lib\router\layer.js:95:5)    at next (C:\Users\lmtla\OneDrive\Desktop\praveenbhai\backend\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\lmtla\OneDrive\Desktop\praveenbhai\backend\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\lmtla\OneDrive\Desktop\praveenbhai\backend\node_modules\express\lib\router\layer.js:95:5)    at C:\Users\lmtla\OneDrive\Desktop\praveenbhai\backend\node_modules\express\lib\router\index.js:281:22
    at param (C:\Users\lmtla\OneDrive\Desktop\praveenbhai\backend\node_modules\express\lib\router\index.js:354:14)
    at paramCallback (C:\Users\lmtla\OneDrive\Desktop\praveenbhai\backend\node_modules\express\lib\router\index.js:401:21)
    at C:\Users\lmtla\OneDrive\Desktop\praveenbhai\backend\controllers\user.js:12:9
    at C:\Users\lmtla\OneDrive\Desktop\praveenbhai\backend\node_modules\mongoose\lib\model.js:4863:16
    at C:\Users\lmtla\OneDrive\Desktop\praveenbhai\backend\node_modules\mongoose\lib\helpers\promiseOrCallback.js:24:16
    at C:\Users\lmtla\OneDrive\Desktop\praveenbhai\backend\node_modules\mongoose\lib\model.js:4886:21
    at C:\Users\lmtla\OneDrive\Desktop\praveenbhai\backend\node_modules\mongoose\lib\query.js:4397:11
    at C:\Users\lmtla\OneDrive\Desktop\praveenbhai\backend\node_modules\kareem\index.js:136:16
    at processTicksAndRejections (internal/process/task_queues.js:75:11)

请帮我找出错误

0 个答案:

没有答案