ReferenceError: err is not defined mongoose mongodb

Viewed 91

When I run this code, it returns an error saying that err is not defined

here is my code

app.post('/tinder/cards', (req, res) => {
const dbCard = req.body;

Cards.create(dbCard, (err, data) => {
    if (err) {
        res.status(500).send(err);
    } else {
        res.status(201).send(data);
    }
});

app.get('/tinder/cards', (req, res) => {
Cards.find(err, data => {
    if (err) {
        res.status(500).send(err);
    } else {
        res.status(200).send(data);
    }
});

Here is my definition of the MongoDB schema:

import mongoose from 'mongoose';

const cardSchema = mongoose.Schema({
    name: String,
    imgUrl: String,
});


export default mongoose.model('cards', cardSchema);

Someone, please help me fix this thank you!

1 Answers

Change

Cards.find(err, data => 

here it's looking for the filter/query as the 1st Parameter which err variable which is not defined so you're getting the ReferenceError.

To

Cards.find(query, (err, data) => { // 1st Parameter should be filter/query

https://mongoosejs.com/docs/api.html#model_Model.find

Model.find()

Parameters

filter «Object|ObjectId»
[projection] «Object|String|Array<String>» optional fields to return, see Query.prototype.select()
[options] «Object» optional see Query.prototype.setOptions()
[callback] «Function»
Related