查询出来是一堆没用的东西呢,为啥不是文档呢:
//db.js var mongoose = require(‘mongoose’); mongoose.connect(‘mongodb://localhost/mybooks’); module.exports = mongoose;
//find.js var mongoose = require(’./db’); var Schema = mongoose.Schema; var booksSchema = new Schema({ name: String, author: String });
var bookModel = mongoose.model(‘ctbook’, booksSchema); console.log(bookModel.find());
{ _mongooseOptions: {}, mongooseCollection: { collection: null, opts: { bufferCommands: true, capped: false }, name: ‘ctbooks’, conn: { base: [Object], collections: [Object], models: {}, replica: false, hosts: null, host: ‘localhost’, port: 27017, user: undefined, pass: undefined, name: ‘mybooks’, options: [Object], otherDbs: [], _readyState: 2, _closeCalled: false, _hasOpened: false, _listening: false, _events: {}, db: [Object] }, queue: [], buffer: true }, model: { [Function: model] base: { connections: [Object], plugins: [], models: [Object], modelSchemas: [Object], options: [Object] }, modelName: ‘ctbook’, model: [Function: model], db: { base: [Object], collections: [Object], models: {}, replica: false, hosts: null, host: ‘localhost’, port: 27017, user: undefined, pass: undefined, name: ‘mybooks’, options: [Object], otherDbs: [], _readyState: 2, _closeCalled: false, _hasOpened: false, _listening: false, _events: {}, 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: ‘ctbooks’, conn: [Object], queue: [], buffer: true } }, op: ‘find’, options: {}, _conditions: {}, _fields: undefined, _update: undefined, _path: undefined, _distinct: undefined, _collection: { collection: { collection: null, opts: [Object], name: ‘ctbooks’, conn: [Object], queue: [], buffer: true } }, _castError: null }
直接在用mongo能查到吗? 如果可以的话。那就是你mongoose用的有问题。 我是这样用的 <pre><code>var meetingSchema = new Schema({ id:Schema.Types.ObjectId, meetingRoomName: String, meetingId:String, meetingSubject:String, members: Schema.Types.Mixed, updated: { type: Date, default: Date.now }, startTime:{ type: Date, default: Date.now }, ownerCorpId:String, ownerName:String }); var meetingModel = mongoose.model(‘meetings’, meetingSchema); var query2 = meetingModel.find(conditions); query1.exec();</code></pre>
你这貌似打出来的是mongoose吧。
@xuwenfei886 少点东西:<pre><code>var mongoose = require(’./db’), _ = require(“underscore”), Schema = mongoose.Schema;</code></pre>
db.js:<pre><code>var mongoose = require(‘mongoose’).connect(‘mongodb://localhost/Titan’); module.exports = mongoose;</code></pre>
Model.find()查出来的就是文档 ,应该是哪里写错了吧 , 上代码瞧瞧
find查询出来的是mongoose的doc对象,支持update等操作,不是POJO,用doc.toObject()转换成POJO
@kenticny 麻烦您看下,代码贴上来了。
@xuwenfei886 我把代码贴上来啦,麻烦您给看一下吧。
@showen 不知道呢,我直接用mongo给查询出来。
@jaycreater -.-mongoose执行find是异步的…返回值是mongoose本身对象 也可以说在nodejs中大多数都是方法都是异步执行的
bookModel.find(function(err, docs) {
// err是错误信息,docs就是查询返回的文档,是一个数组
console.log(docs);
});
bookModel.find({},[],{},function(err,docs){ console.log(docs); })
@kenticny 但是,我要怎么把docs给视图使用呢。
@jiangli373 这样控制台是可以打印出来,可是怎么给视图使用呢?因为,我主要是查出来给视图模板使用的。
@jaycreater 以express中为例
bookModel.find(function(err, docs) {
if(err) {
console.error(err);
return;
}
res.render('view', {
list: docs
});
});
@kenticny 嗯嗯,谢谢啦。
@jaycreater 楼上有人给出答案了