我正在尝试获取所有mongoose模型名称并在视图中迭代它们(test.jade
)
路线
router.get('/dashboard', ensureAuthenticated, function (req, res) {
var models = mongoose.models;
console.log(models);
res.render('test',{data:models});
})
我在节点控制台上获得了这个日志:
{ User:
{ [Function: model]
base:
{ connections: [Object],
plugins: [],
models: [Circular],
modelSchemas: [Object],
options: [Object] },
modelName: 'User',
model: [Function: model],
db:
{ base: [Object],
collections: [Object],
models: [Circular],
replica: false,
hosts: null,
host: 'localhost',
port: 27017,
user: '',
pass: '',
name: 'simpleblog',
options: [Object],
otherDbs: [],
_readyState: 1,
_closeCalled: false,
_hasOpened: true,
_listening: true,
_events: {},
db: [Object] },
discriminators: undefined,
schema:
{ paths: [Object],
subpaths: {},
virtuals: [Object],
nested: {},
inherits: {},
callQueue: [],
_indexes: [],
methods: {},
statics: {},
tree: [Object],
_requiredpaths: [],
discriminatorMapping: undefined,
_indexedpaths: undefined,
options: [Object],
_events: {} },
options: undefined,
collection:
{ collection: [Object],
opts: [Object],
name: 'users',
conn: [Object],
queue: [],
buffer: false } },
Comment:
{ [Function: model]
base:
{ connections: [Object],
plugins: [],
models: [Circular],
modelSchemas: [Object],
options: [Object] },
modelName: 'Comment',
model: [Function: model],
db:
{ base: [Object],
collections: [Object],
models: [Circular],
replica: false,
hosts: null,
host: 'localhost',
port: 27017,
user: '',
pass: '',
name: 'simpleblog',
options: [Object],
otherDbs: [],
_readyState: 1,
_closeCalled: false,
_hasOpened: true,
_listening: true,
_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: [Object],
opts: [Object],
name: 'comments',
conn: [Object],
queue: [],
buffer: false } },
Article:
{ [Function: model]
base:
{ connections: [Object],
plugins: [],
models: [Circular],
modelSchemas: [Object],
options: [Object] },
modelName: 'Article',
model: [Function: model],
db:
{ base: [Object],
collections: [Object],
models: [Circular],
replica: false,
hosts: null,
host: 'localhost',
port: 27017,
user: '',
pass: '',
name: 'simpleblog',
options: [Object],
otherDbs: [],
_readyState: 1,
_closeCalled: false,
_hasOpened: true,
_listening: true,
_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: [Object],
opts: [Object],
name: 'articles',
conn: [Object],
queue: [],
buffer: false } } }
查看
for(var prop in data)
p #{prop}: #{data[prop]}
问题是我在视图
的迭代操作后无法获得任何结果答案 0 :(得分:2)
在你的路线中,试试这个:
router.get('/dashboard', ensureAuthenticated, function (req, res) {
var models = Object.keys(mongoose.models);
var schemas = mongoose.modelSchemas;
res.render('test',{models: models, schemas: schemas});
})
然后在你的布局文件中试试这个(假设你正在使用玉)
each model in models
p #{model} :
each path in schemas[model].paths
div #{JSON.stringify(path)}
答案 1 :(得分:0)
很难说,但看起来您可能正在记录构建模型的函数,而不是模型数据的实例。