Xiangxuan Qu
Xiangxuan Qu

Reputation: 153

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

I use the method by Bookshelfjs.org to query some datas. But there is a bug! This is my code.

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

The database is mysql,and I want to use sql like

select * from defray where purchaseId = 2;

Bug info is

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! [email protected] start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] 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

The sql can run at my cmd,but I can`t use bookshelf to achieve my target. Help me,please!

Upvotes: 0

Views: 1508

Answers (1)

flaviodesousa
flaviodesousa

Reputation: 7815

Queries are a model feature not an instance one. So just change your code to query directly on the Defray model, like:

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

Upvotes: 1

Related Questions