“describe”和“schema.define”有什么区别?

时间:2013-04-27 16:36:32

标签: node.js compoundjs jugglingdb

当我通过CompoundJS的世界取得进展时,我发现了两种定义模式的方法:

首先:

var Product = describe('Product', function () {
  property('upc', String);
  property('name', String);
  set('restPath', pathTo.products);
});

第二

var Schema = require('jugglingdb').Schema;
var schema = new Schema('memory');

var Product = schema.define('Product', {
  upc: { type: Number, index: true },
  name: { type: String, limit: 150, index: true },
  createdAt: { type: Date, default: Date.now },
  modifiedAt: { type: Date, default: Date.now }
}, {
  restPath: pathTo.products
});

第一个,有效,但看起来像旧设计。第二种方法不起作用,但这是根据JugglingDB文档进行操作的方法。

我应该使用哪一个?为什么第二个不适合我?

更新 这是我使用第二个时得到的错误:

Express
500 TypeError: Cannot call method 'all' of undefined in products controller during "index" action

at Object.index (C:\Users\Eran\Documents\Relay Foods\nutrition-facts-editor\app\controllers\products.js:47:13)
at Array.2 (C:\Users\Eran\Documents\Relay Foods\nutrition-facts-editor\node_modules\compound\node_modules\kontroller\lib\flow-control.js:150:28)
at ActionContext.run [as innerNext] (C:\Users\Eran\Documents\Relay Foods\nutrition-facts-editor\node_modules\compound\node_modules\kontroller\lib\flow-control.js:103:31)
at Controller.BaseController.next (C:\Users\Eran\Documents\Relay Foods\nutrition-facts-editor\node_modules\compound\node_modules\kontroller\lib\base.js:107:22)
at Controller.protectFromForgery (C:\Users\Eran\Documents\Relay Foods\nutrition-facts-editor\node_modules\compound\node_modules\kontroller\lib\helpers.js:76:21)
at Object.protectFromForgeryHook (C:\Users\Eran\Documents\Relay Foods\nutrition-facts-editor\app\controllers\application.js:3:13)
at Array.1 (C:\Users\Eran\Documents\Relay Foods\nutrition-facts-editor\node_modules\compound\node_modules\kontroller\lib\flow-control.js:150:28)
at run (C:\Users\Eran\Documents\Relay Foods\nutrition-facts-editor\node_modules\compound\node_modules\kontroller\lib\flow-control.js:103:31)
... snip ...

1 个答案:

答案 0 :(得分:1)

我认为describedefine是相同的。这里的问题是我的第一个实现中的全局使用范围和第二个实现中的本地使用范围。所以我需要它是全局的才能与应用程序的其余部分一起工作。