我无法在我的应用中获取mongoose查询的结果。通过mongo shell我可以跑
db.statements.find({"name":"CASH HSBC"})
并获得正确的结果,但是当我通过下面的代码运行等效项时
和console.log()
我得到的回复[]
并且我不明白为什么。
我有一个模型
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);
然后在我的路线js中我
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;
供应商手柄模板不会呈现任何内容,但我猜测是因为doc
的值为空。
编辑:关于评论中的请求以进行简单查询。
如果我将以下内容添加到我的路线
var db = Statement.find();
console.log(db);
我在控制台中获得了一个大对象(它确实引用了我的收藏)
Query {
_mongooseOptions: {},
mongooseCollection:
NativeCollection {
collection: Collection { s: [Object] },
opts: { bufferCommands: true, capped: false },
name: 'statements',
collectionName: 'statements',
conn:
NativeConnection {
base: [Object],
collections: [Object],
models: [Object],
config: [Object],
replica: false,
hosts: null,
host: 'localhost',
port: 27017,
user: undefined,
pass: undefined,
name: 'finance',
options: [Object],
otherDbs: [],
_readyState: 1,
_closeCalled: false,
_hasOpened: true,
_listening: false,
db: [Object] },
queue: [],
buffer: false,
emitter:
EventEmitter {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined } },
model:
{ [Function: model]
hooks: Kareem { _pres: {}, _posts: {} },
base:
Mongoose {
connections: [Object],
plugins: [],
models: [Object],
modelSchemas: [Object],
options: [Object] },
modelName: 'Statement',
model: [Function: model],
db:
NativeConnection {
base: [Object],
collections: [Object],
models: [Object],
config: [Object],
replica: false,
hosts: null,
host: 'localhost',
port: 27017,
user: undefined,
pass: undefined,
name: 'finance',
options: [Object],
otherDbs: [],
_readyState: 1,
_closeCalled: false,
_hasOpened: true,
_listening: false,
db: [Object] },
discriminators: undefined,
schema:
Schema {
obj: [Object],
paths: [Object],
subpaths: {},
virtuals: [Object],
singleNestedPaths: {},
nested: {},
inherits: {},
callQueue: [Object],
_indexes: [],
methods: {},
statics: {},
tree: [Object],
query: {},
childSchemas: [],
s: [Object],
options: [Object],
'$globalPluginsApplied': true },
collection:
NativeCollection {
collection: [Object],
opts: [Object],
name: 'statements',
collectionName: 'statements',
conn: [Object],
queue: [],
buffer: false,
emitter: [Object] },
Query: { [Function] base: [Object] },
'$__insertMany': [Function],
insertMany: [Function] },
schema:
Schema {
obj: { date: [Object], name: [Object], amount: [Object] },
paths:
{ date: [Object],
name: [Object],
amount: [Object],
_id: [Object],
__v: [Object] },
subpaths: {},
virtuals: { id: [Object] },
singleNestedPaths: {},
nested: {},
inherits: {},
callQueue: [ [Object], [Object], [Object], [Object] ],
_indexes: [],
methods: {},
statics: {},
tree:
{ date: [Object],
name: [Object],
amount: [Object],
_id: [Object],
id: [Object],
__v: [Function: Number] },
query: {},
childSchemas: [],
s: { hooks: [Object], kareemHooks: [Object] },
options:
{ retainKeyOrder: false,
typeKey: 'type',
id: true,
noVirtualId: false,
_id: true,
noId: false,
validateBeforeSave: true,
read: null,
shardKey: null,
autoIndex: null,
minimize: true,
discriminatorKey: '__t',
versionKey: '__v',
capped: false,
bufferCommands: true,
strict: true,
pluralization: true },
'$globalPluginsApplied': true },
op: 'find',
options: { retainKeyOrder: false },
_conditions: {},
_fields: undefined,
_update: undefined,
_path: undefined,
_distinct: undefined,
_collection:
NodeCollection {
collection:
NativeCollection {
collection: [Object],
opts: [Object],
name: 'statements',
collectionName: 'statements',
conn: [Object],
queue: [],
buffer: false,
emitter: [Object] },
collectionName: 'statements' },
_traceFunction: undefined,
_castError: null,
_count: [Function],
_execUpdate: [Function],
_find: [Function],
_findOne: [Function],
_findOneAndRemove: [Function],
_findOneAndUpdate: [Function],
_replaceOne: [Function],
_updateMany: [Function],
_updateOne: [Function] }