Friday 21 April 2017

mongoose - empty result but fine in mongo shell

I'm having trouble getting results from a mongoose query within my app. Via the mongo shell i can run db.statements.find({"name":"CASH HSBC"}) and get the correct result but when I run the equivalent through the code below and console.log() the response I get [] and I don't understand why.

I have a model below

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var statementSchema = new Schema({
date : {type:Date, required:true},
name: {type:String, required:true},
amount: {type:Number, required:true}
});

module.exports = mongoose.model('Statement', statementSchema);

and then in my the route js I have

var express = require('express');
var router = express.Router();
var parseDate = require('../helpers/parseDate');
var mongoose = require('mongoose');
var Statement = require('../models/statement');


/* GET home page. */
router.get('/', function(req, res, next) {
res.send('You need to add a value');
});

router.get('/:vendor?', function(req, res, next) {

var vendorName = req.params.vendor;

Statement.find({name:req.params.vendor}, function(err, doc){
   if (err) {
       console.error('error no entries found');
   }
    console.log(doc);
    res.render('vendor', {
        vendor: vendorName,
        shop:doc
    });
});

});

module.exports = router;

the vendor handlebars template doesn't render anything but I'm guessing that's because the value of doc is empty



via Stuart Brown

No comments:

Post a Comment