[英]Pulling all documents from collection with mongoose
编辑:我的错误是从未实际使用/spells
的$http.get
调用,所以router.get
从未发生。
在我的app.js
,我有app.js
行...
var mongoose = require('mongoose');
mongoose.connect('...');
require('./models/spells');
var routes = require('./routes/index');
spells.js
定义了SpellSchema
...
var mongoose = require('mongoose');
var SpellSchema = new mongoose.Schema({
name: String,
level: String,
school: String,
ritual: String,
classes: [String],
castingTime: String,
range: String,
components: [String],
duration: String,
description: [String],
page: String
}, {});
mongoose.model('spell', SpellSchema);
然后在index.js
我...
var mongoose = require('mongoose');
var test = mongoose.model('spell');
router.get('/spells', function(req, res, next) {
test.find({}, function(err, data) {
console.log(err, data, data.length);
if (err) { return next(err); }
res.json(data);
});
});
但是console.log
输出任何内容。 如果在spells.js
结尾,我有...
var test = mongoose.model('spell', SpellSchema);
test.find({}, function(err, data) {
console.log(err, data, data.length);
}
...所有拼写文档都打印到控制台上。
我对为什么会这样感到有些困惑? 据我所知,它应该打印咒语。
试试这个
data = JSON.stringify(data);
data = JSON.parse(data);
res.json(data);
你有尝试过吗?
var SpellModel = require('./shell');
SpellModel.find({}, console.log);
我不认为您应该调用mongoose.model,因为该mongo数据库连接可能尚不可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.