【问题标题】:ReferenceError Articles is not definedReferenceError 文章未定义
【发布时间】:2016-01-30 09:21:38
【问题描述】:

我定义了几个模式。这是一个运行良好的方法:

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();

我收到主题中的错误。在跟踪代码时,我可以看到模块在范围内,所以我不相信它是愚蠢的,比如重新定义变量或类似的东西。

这里发布了几个此类性质的问题,但没有一个被接受的答案适用。

有什么想法吗?

【问题讨论】:

  • 为什么在你的代码中有这些随机空格?我的朋友,你肯定需要一个 linter :)
  • 这里是粘贴到代码窗口,然后缩进让代码显示为代码的结果。

标签: node.js mongodb mongoose


【解决方案1】:

好吧,经过进一步分析,工作“模型”不再起作用。事实证明,我根据 Visual Studio Code 的建议更改了顶部 require 语句中的大小写。现在一切都很好。

【讨论】:

    【解决方案2】:

    而不是 sourceId: [ {type: Schema.Types.ObjectId, ref: NewsSchema}] 采用 sourceId: [ {type: Schema.Types.ObjectId, ref: 'NewsSchema'}] 会解决你的问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-24
      • 2019-09-21
      • 2021-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-11
      • 2018-09-26
      相关资源
      最近更新 更多