Home > Blockchain >  Why the find() method of mongoose return me err?
Why the find() method of mongoose return me err?

Time:03-12

The server return me an empty json, I think there is some problem with the mongoose method find() that doesn't find nothing. Here's the code:

inde.js:

const express = require('express');
const router = express.Router();

const Folders = require('../bin/models/folders');

router.get('/:id', function (req, res) {
    Folders.find({ 'BeaconId': req.params.id }).exec((err, folders) => {
        if (err) return res.status(500).json({ error: err });
        if (!folders) return res.status(404).json({ message: 'Folder not found' });
        res.json(folders);
    });
});
module.exports = router;

folders.js:

onst mongoose = require('mongoose');

const folderSchema = mongoose.Schema({
    Name_folder: String,
    Link_folder: String,
    BeaconId: Number
});

const Folders = mongoose.model('Folders', folderSchema, 'folders');

module.exports = Folders;

CodePudding user response:

Query params are always a string, so you can try to change the BeaconId from Number to String in the mongooseSchema:

const folderSchema = mongoose.Schema({
    Name_folder: String,
    Link_folder: String,
    BeaconId: String
});

or alternatively you can convert the string query.params.id to number:

{ 'BeaconId': parseInt(req.params.id)}
  • Related