MongoError: filter parameter must be an object

12,881

Use $eq operator in the filter object $eq

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

So the final snippet becomes like this:

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]);
        }
    });

});
Share:
12,881

Related videos on Youtube

Hot Zellah
Author by

Hot Zellah

Updated on June 04, 2022

Comments

  • Hot Zellah
    Hot Zellah almost 2 years

    I am creating a rest api I have end point for Post/Movies: Request body should contain only movie title, and its presence should be validated Based on passed title, other movie details should be fetched from thememoviedb,and saved to application database.

    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]);
                }
            });
    });
    

    when I run the app I get this error, what am I doing wrong here,? am new to nodejs and all this stuff just learning

    • Neil Lunn
      Neil Lunn almost 6 years
      req.body.title is not an "object". Actually it looks like a typo as the next thing { title: 'Avengers' } is what you mean. Or probably { title: req.body.title }. In short, it looks like you forgot to remove your test and added the variable in the wrong place.
  • Hot Zellah
    Hot Zellah almost 6 years
    when I run localhost: 8000/movies i get errror in console TypeError: Cannot read property '0' of undefined , in post man , could not get any rensponse.
  • Adam Azad
    Adam Azad almost 6 years
    Try console.log(result) to see how the JSON is structured
  • Hot Zellah
    Hot Zellah almost 6 years
    This is what I get : We are live on 8000 { lastErrorObject: { updatedExisting: false, n: 0 }, value: null, ok: 1 } error: null statusCode: 200
  • Hot Zellah
    Hot Zellah almost 6 years
    Unexpected Token , } ?
  • Sajeetharan
    Sajeetharan almost 6 years
    probably i have not closed it properly, check the closing { in your code
  • Hot Zellah
    Hot Zellah almost 6 years
    something is not right with set method, check it again if that is the right way of doing it , closing brackets all good
  • Sajeetharan
    Sajeetharan almost 6 years
  • Sajeetharan
    Sajeetharan almost 6 years
    did the answer help?
  • Hot Zellah
    Hot Zellah almost 6 years
    I went ti sleep I will check it up :)
  • Sajeetharan
    Sajeetharan over 5 years
    @HotZellah did it help?