javascript 使用Mongoose将数据插入MongoDB

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 使用Mongoose将数据插入MongoDB相关的知识,希望对你有一定的参考价值。

//========= POST ========//


// Process Form
app.post('/ideas', (req, res) => {
    let errors = [];

    // Add Server-Side Validation
    if (!req.body.title) {
        errors.push({
            text: 'Please add a title'
        });
    }
    if (!req.body.details) {
        errors.push({
            text: 'Please add some details'
        });
    }

    // Check if there are errors
    if (errors.length > 0) {
        // Render back html page to send error messages
        res.render('ideas/add', {
            errors,
            title: req.body.title,
            details: req.body.details
        })
    } else {
        // Shit happens here
      
        const newUser = {
            title: req.body.title,
            details: req.body.details
        }

        // Don't just fucking insert the request body in here.
        // It's not a good idea
        new Idea(newUser)
            .save()
            .then(idea => {
                 res.redirect('/ideas');
            })
    }


});


//===== END POST ====//

以上是关于javascript 使用Mongoose将数据插入MongoDB的主要内容,如果未能解决你的问题,请参考以下文章