簡體   English   中英

MongoError:篩選器參數必須是一個對象

[英]MongoError: filter parameter must be an object

我正在創建一個REST API,我有Post / Movies的終點:請求正文應僅包含電影標題,並且應該驗證其存在基於傳遞的標題,其他電影詳細信息應從thememoviedb中獲取,並保存到應用程序數據庫中。

app.post('/movies', (req, res) => {
        request('https://api.themoviedb.org/3/discover/movie?callback=JSONP_CALLBACK&sort_by=popularity.desc&api_key=2931998c3a80d7806199320f76d65298', function (error, response, body) {
            console.log('error:', error); // Print the error if one occurred and handle it
            console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received

          });
        db.collection('movies').findOneAndUpdate(req.body.title,{
            title: 'Avengers',
        },(err, result) => {
            if (err) {
                res.send({
                    'error': 'An error has occured'
                });
            } else {
                res.send(result.ops[0]);
            }
        });
});

當我運行應用程序時,出現此錯誤,我在這里做錯了什么? 是nodejs的新手,所有這些東西都只是在學習

在過濾器對象$eq使用$eq運算符

{ <field>: { $eq: <value> } }

因此,最終的代碼段如下所示:

app.post('/movies', (req, res) => {

    /* code ... */

    let { title } = req.body

    db.collection('movies').findOneAndUpdate({ title: { $eq: title } }, { title: 'Avengers' }, (err, result) => {
        if (err) {
            res.send({ 'error': 'An error has occured' });
        } else {
            res.send(result.ops[0]);
        }
    });

});

嘗試以下方法

db.collection('movies').findOneAndUpdate({title:req.body.title},{
          $set:{
           'Avengers'
          }})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM