猫鼬/表达式无法读取未定义的属性“ unshift”

时间:2019-05-10 15:40:12

标签: express mongoose

下面是关于udemy的教程,遇到了此错误。 “无法读取未定义的属性'unshift'”。香港专业教育学院以前有这个问题,但这是因为我忘记了对json网络令牌的身份验证。这次已通过身份验证,并且代码是一个接一个字母地检查,并且完全相同。我们都知道100%的代码教程已经过时并且缺少信息。

已检查并验证了身份验证。字段已检查并验证。我看到的一个方面是“经验”没有“构造函数”?

router.put(
    '/experience', 
    [
        auth, 
        [
            check('title', 'Title is required')
                .not()
                .isEmpty(),
            check('company', 'Company is required')
                .not()
                .isEmpty(),
            check('from', 'From date is required')
                .not()
                .isEmpty()
        ]
    ],

async (req, res) => {
        const errors = validationResult(req);
        if (!errors.isEmpty()) {
            return res.status(400).json({errors: errors.array() });
        }


        const {
            title,
            company,
            location,
            from,
            to,
            current,
            description
        } = req.body;


        const newExp = {
            title,
            company,
            location,
            from,
            to,
            current,
            description
        }

        //const experience = [];

        try{
            const profile = await Profile.findOne({ user: req.user.id });

            profile.experience.unshift(newExp);

            await profile.save();

            res.json(profile);

        } catch(err){
            console.error(err.message);
            res.status(500).send('server put exp error')
        }
    }

1 个答案:

答案 0 :(得分:0)

我正在同一门课程上,我的解决方案是更改线路

profile.experience.unshift(newExp);

profile.experiences.unshift(newExp);

这是它在数据库中的保存方式。

相关问题