In the case of the “no result” scenario in Express, how can I display a selection error message in the browser without displaying an empty array []

var express = require('express');
var router = express.Router();
var Questionnaire = require('../models/questionnaire');




router.get('/', function(req, res, next) {
 Questionnaire.find().sort('username').exec(function(error, results) {
 if (error) {
 return next(error);
 }
 // Respond with valid data
 res.json(results);
 });
 });
 router.get('/:questionnaireId', function(req, res, next){
Questionnaire
.where({username: req.params.questionnaireId}, {featured: true})
.where({featured: true})
.exec(function(error, results){
 if (error) {
 return next(error);
 }
 if (!results) {
res.send(404);
}
res.json(results);
});
});

module.exports = router;

In the case of the "no result" scenario in Express, how can I display a selection error message in the browser without displaying an empty array []. Each time I run a query using the Express platform, my browser always displays this empty array [] when the result is not found or the user is not found. How can I get around this?

+4
source share
1 answer
[...]

if (error) {
  return next(error);
}

// Check for empty results
if (!results.length) {
  results = { ... }; //custom response object
}

// Respond with valid data
res.json(results);

[...]
0
source

Source: https://habr.com/ru/post/1653893/


All Articles