问:Bookshelf.js:如何通过bookshelfjs查询?

时间:2016-03-17 09:36:49

标签: mysql node.js bookshelf.js

我使用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可以在我的cmd上运行,但是我不能使用书架来实现我的目标。 请帮帮我!

1 个答案:

答案 0 :(得分:1)

查询是模型功能而非实例功能。因此,只需将代码更改为直接在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);
});