Q:Bookshelf.js:bookshelfjs如何查询?

Q:Bookshelf.js:How to query by bookshelfjs?

我使用Bookshelfjs.org的方法查询了一些数据。 但是有一个错误! 这是我的代码。

var Defray = db.Model.extend({
    tableName: 'defray',
    idAttribute: 'id'
});
new Defray.query('where', 'purchaseId', '=', '2').fetch().then(function(model) {
    console.log(model);
});

数据库是mysql,我想用sql喜欢

select * from defray where purchaseId = 2;

错误信息是

var model = this.forge();
                       ^

TypeError: this.forge is not a function
    at new Model.(anonymous function).Collection.(anonymous function) (E:\wordspace\javascript\HEKR_END\node_modules\bookshelf\lib\bookshelf.js:329:24)
    at Object.<anonymous> (E:\wordspace\javascript\HEKR_END\database\model.js:30:1)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:313:12)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    at Object.<anonymous> (E:\wordspace\javascript\HEKR_END\routes\index.js:6:13)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:313:12)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    at Object.<anonymous> (E:\wordspace\javascript\HEKR_END\app.js:15:14)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:313:12)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)

npm ERR! Windows_NT 10.0.10586
npm ERR! argv "C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" "run-script" "start"
npm ERR! node v4.2.4
npm ERR! npm  v2.14.12
npm ERR! code ELIFECYCLE
npm ERR! app9d@0.0.0 start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the app9d@0.0.0 start script 'node ./bin/www'.
npm ERR! This is most likely a problem with the app9d package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node ./bin/www
npm ERR! You can get their info via:
npm ERR!     npm owner ls app9d
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     E:\wordspace\javascript\HEKR_END\npm-debug.log

sql 可以在我的命令中 运行,但我不能使用书架来实现我的目标。 请帮帮我!

查询是一种模型功能,而不是实例功能。所以只需更改您的代码以直接在 Defray 模型上查询,例如:

var Defray = db.Model.extend({
    tableName: 'defray'  // id not needed, is the default PK name
});
Defray.query('where', 'purchaseId', '=', '2').fetch().then(function(model) {
    console.dir(model);
});