node.js 要求返回一个空对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23875233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:24:00  来源:igfitidea点击:

Require returns an empty object

node.js

提问by user3677173

I have a folder, that has index.js and a couple of models (classes) index.js

我有一个文件夹,里面有 index.js 和几个模型(类)index.js

module.exports = {
   Book : require('./book'),
   Author : require('./author')
}

book.js

书.js

var Author = require('./author')
var Book = models.ActiveRecord.extend({
    schema : {
        belongsTo : {
            author : Author
        }
    }
})
module.exports = Book

author.js

作者.js

var Book = require('./book')
var Author = models.ActiveRecord.extend({
    schema : {
        hasMany : {
            author : Book
        }
    }
})

module.exports = Author

The problem is that Author class does not seem to find the Book! It's just an empty Object. However, if I switch the exports in index.js, putting Book after Author - it works, but then the other model stops working.

问题是 Author 类似乎没有找到 Book!它只是一个空对象。但是,如果我在 index.js 中切换导出,将 Book 放在 Author 之后 - 它可以工作,但另一个模型会停止工作。

I don't want to do any hacks to make it work.

我不想做任何黑客来让它工作。

回答by B T

This is because you have a circular dependency. Node.js handles this in a very specific way:

这是因为您有循环依赖。Node.js 以一种非常具体的方式处理这个问题:

  1. The first module loads and runs (in this case, book.js). It (book.js) will load and run the second module (author.js) when it (book.js) requires the other (author.js)

  2. When the second module (author.js) is loaded and run, it (author.js) requires the first module (book.js) but it (author.js) will receive a partially filledobject - however many things were set on the exports in book.js before it required author.js will be in that object

  3. After book.jsis completely run through, the object author.jsgot from require('./book')will be the full book.jsmodule object

  1. 第一个模块加载并运行(在本例中为book.js)。它 ( book.js) 将加载并运行第二个模块 ( author.js) 当它 ( book.js) 需要另一个 ( author.js)

  2. 当第二个模块 ( author.js) 加载并运行时,它 ( author.js) 需要第一个模块 ( book.js) 但它 ( author.js) 将收到一个部分填充的对象 - 但是在 book.js 中的导出上设置了很多东西,然后才需要 author.js在那个对象中

  3. book.js完全运行后,author.js从中得到的对象require('./book')将是完整的book.js模块对象

For more info, here's the docs: http://nodejs.org/api/modules.html

有关更多信息,请参阅文档:http: //nodejs.org/api/modules.html

If its possible to dynamically add that schema to one of those ActiveRecord objects, that's one way to solve this. This is actually kind of a tricky situation. In fact, even without the module system, this would cause problems for you. If you put all this code in one file, how would you make it work?

如果可以将该模式动态添加到这些 ActiveRecord 对象之一,这是解决此问题的一种方法。这实际上是一种棘手的情况。事实上,即使没有模块系统,这也会给你带来问题。如果你把所有这些代码放在一个文件中,你会如何让它工作?