我定义了几个模式。这是一个很好的工作:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var NewsSchema = new Schema({
name: String,
route: String,
remoteURL: String,
articles: [{title: String, link: String, Image: String, contentSnippet: String}],
retrieved: Date,
dormant: { type: Boolean, default: false}
});
module.exports = mongoose.model('News', NewsSchema);
这是第二个几乎相同的人:
var mongoose = require('mongoose'),
Schema = mongoose.Schema
// NewsSchema = new Schema({ name: String });
var ArticlesSchema = new Schema({
title: String,
link: String,
pubDate: Date,
image: String,
contentSnippet: String,
sourceName: String
// sourceId: [ {type: Schema.Types.ObjectId, ref: NewsSchema}]
});
module.exports = mongoose.model('Articles', ArticlesSchema);
我已经加载了程序顶部的两个模块,以及其他一些类似的东西:
players = require('./app/models/players'),
articles = require('./app/models/articles'),
如果我用以下内容创建第一个实例:
var player = new Players();
但是如果我尝试用:
创建第二个实例var item = new Articles();
我收到主题中的错误。在跟踪代码时,我可以看到模块在范围内,所以我不相信它像重新定义变量或类似的东西一样愚蠢。
有什么想法吗?
答案 0 :(得分:1)
缺少引号,所以 而不是
sourceId: [ {type: Schema.Types.ObjectId, ref: NewsSchema}]
使用
sourceId: [ {type: Schema.Types.ObjectId, ref: 'NewsSchema'}]
可以解决您的问题。