mongoose find返回结果不是文档, 求救!!
发布于 10 年前 作者 PomL 6112 次浏览 最后一次编辑是 8 年前 来自 问答

上测试代码: var express = require(‘express’); var router = express.Router(); var mongoose = require(‘mongoose’);

mongoose.connect(‘mongodb://localhost/microblog’); var db = mongoose.connection; db.on(‘error’, console.error.bind(console, ‘connection error:’)); db.once(‘open’, function(callback) { console.log(‘success!’); }); var firstSchema = mongoose.Schema({ name: String }); var demo = mongoose.model(‘demo’, firstSchema); var test = demo.find({ name: ‘demos’}); console.log(test);

这样在终端居然打印的是这些东西… { _mongooseOptions: {}, mongooseCollection: { collection: null, opts: { bufferCommands: true, capped: false }, name: ‘demos’, conn: { base: [Object], collections: [Object], models: [Object], replica: false, hosts: null, host: ‘localhost’, port: 27017, user: undefined, pass: undefined, name: ‘microblog’, options: [Object], otherDbs: [], _readyState: 2, _closeCalled: false, _hasOpened: false, _listening: false, _events: [Object], db: [Object] }, queue: [], buffer: true }, model: { [Function: model] base: { connections: [Object], plugins: [], models: [Object], modelSchemas: [Object], options: [Object] }, modelName: ‘demo’, model: [Function: model], db: { base: [Object], collections: [Object], models: [Object], replica: false, hosts: null, host: ‘localhost’, port: 27017, user: undefined, pass: undefined, name: ‘microblog’, options: [Object], otherDbs: [], _readyState: 2, _closeCalled: false, _hasOpened: false, _listening: false, _events: [Object], db: [Object] }, discriminators: undefined, schema: { paths: [Object], subpaths: {}, virtuals: [Object], nested: {}, inherits: {}, callQueue: [], _indexes: [], methods: {}, statics: {}, tree: [Object], _requiredpaths: undefined, discriminatorMapping: undefined, _indexedpaths: undefined, options: [Object], _events: {} }, options: undefined, collection: { collection: null, opts: [Object], name: ‘demos’, conn: [Object], queue: [], buffer: true } }, op: ‘find’, options: {}, _conditions: { name: ‘demos’ }, _fields: undefined, _update: undefined, _path: undefined, _distinct: undefined, _collection: { collection: { collection: null, opts: [Object], name: ‘demos’, conn: [Object], queue: [], buffer: true } }, _castError: null }

新手求教啊啊啊啊啊啊!!!

9 回复
demo.find({ name: 'demos’},function(err,doc){
 if(err) console.error(err);
 console.log(doc);
});

@jiangli373 这样可以返回,但有些场景不希望使用回调函数,按官方文档不使用回调函数也是可以返回结果集的。为什么我返回的是些奇怪的东西

官方哪个文档是说不用回调也能返回的

除非有同步查询,不然是不行的。

find返回的是数组,没有结果时返回的是空数组

@PomL 你这个返回的是find这个方法吧,另外就是可否把官方文档不使用回调函数的地址贴一下

你这里就是个query啊,还没有执行exec又怎么有结果呢?

exec 也要回调

回到顶部