Home > database >  Parameter "filter" to find() must be an object
Parameter "filter" to find() must be an object

Time:11-09

hey everyone I'm having this error (MEAN STACK)

  • Failed to load resource: the server responded with a status of 500 (Internal Server Error)
  • Parameter "filter" to find() must be an object, got 1984"

This is the backend code:

    booksRoute.route('/:name').get((req,res,next)=>{
        BookModel.find(req.params.name,(error,data)=>{
            if (error){
                return next(error) ; 
            } else {
                res.json(data)
            }
        })
    })

just a simple API to get a book by it's name that's all and here's the frontend code (form with one single input for the book name)

    <form  method="GET" class="d-flex" #f="ngForm" (ngSubmit)="searchBook(f.value)">
    <input name="searchedBook" class="form-control me-2" type="text" placeholder="search book by name" aria-label="Search" ngModel>
    <button class="btn btn-outline-success" type="submit">Search</button>
    </form>

and here's the service I'm using to get data

    getBook(name){
        return this.http.get(this.backendUrl '/' name) ; 
      }

and here's the mongoose data model

    let books = new Schema({
        name:{
            type:String
        },
        genre:{
            type:String
        },
        author:{
            type:String
        },
        rating:{
            type:Number
        },
        price:{
            type:Number
        }
    },{
        collection: 'booklist' 
    })

CodePudding user response:

You should pass object with property name to the .find(), and you have passed req.params.name. Change your code like this:

BookModel.find({ name: req.params.name },(error,data)=>{
  • Related