我是bookshelf.js的新手,并且在我的node.js + MySQL项目中将它用于ORM。 这个简单的查询我收到一个错误。有人能告诉我哪里出错了吗?
//Get reviews for a service - TESTED
app.get('/api/review/:serviceID?', function(req, res) {
var serviceID = req.params.serviceID;
console.log("Service ID: " + serviceID);
new Review.query('where','serviceID','=', serviceID)
.fetchAll()
.then(function(reviews) {
if (reviews!=null)
res.send(reviews.toJSON());
else
res.send("No reviews found");
}).catch(function(error) {
console.log(error);
res.send('An error occurred in fetching reviews for service '+ serviceID);
});
});
此处查看表的主键为reviewID,serviceID为外键。 这是我收到的错误消息。
TypeError: Object [object Object] has no method 'forge'
at new Model.(anonymous function).Collection.(anonymous function
(d:\PMF\node_modules\bookshelf\bookshelf.js:146:24)
at app.get.serviceID (d:\PMF\app.js:121:15)
at Layer.handle [as handle_request]
(d:\PMF\node_modules\express\lib\router\layer.js:82:5)
at next (d:\PMF\node_modules\express\lib\router\route.js:110:13)
at Route.dispatch (d:\PMF\node_modules\express\lib\router\route.js:91:3)
at Layer.handle [as handle_request
(d:\PMF\node_modules\express\lib\router\layer.js:82:5)
at d:\PMF\node_modules\express\lib\router\index.js:267:22
at param (d:\PMF\node_modules\express\lib\router\index.js:340:14)
at param (d:\PMF\node_modules\express\lib\router\index.js:356:14)
at Function.proto.process_params
(d:\PMF\node_modules\express\lib\router\index.js:400:3)
at next (d:\PMF\node_modules\express\lib\router\index.js:261:10)
at jsonParser (d:\PMF\node_modules\body-parser\lib\types\json.js:96:40)
at Layer.handle [as handle_request (d:\PMF\node_modules\express\lib\router\layer.js:82:5)
at trim_prefix (d:\PMF\node_modules\express\lib\router\index.js:302:13)
at d:\PMF\node_modules\express\lib\router\index.js:270:7
at Function.proto.process_params (d:\PMF\node_modules\express\lib\router\index.js:321:12)
at next (d:\PMF\node_modules\express\lib\router\index.js:261:10)
at jsonParser (d:\PMF\node_modules\body-parser\lib\types\json.js:96:40)
at Layer.handle [as handle_request] (d:\PMF\node_modules\express\lib\router\layer.js:82:5)
at trim_prefix (d:\PMF\node_modules\express\lib\router\index.js:302:13)
at d:\PMF\node_modules\express\lib\router\index.js:270:7
at Function.proto.process_params (d:\PMF\node_modules\express\lib\router\index.js:321:12)
at next (d:\PMF\node_modules\express\lib\router\index.js:261:10)
at urlencodedParser (d:\PMF\node_modules\body-parser\lib\types\urlencoded.js:84:40)
at Layer.handle [as handle_request] (d:\PMF\node_modules\express\lib\router\layer.js:82:5)
at trim_prefix (d:\PMF\node_modules\express\lib\router\index.js:302:13)
at d:\PMF\node_modules\express\lib\router\index.js:270:7
at Function.proto.process_params (d:\PMF\node_modules\express\lib\router\index.js:321:12)
答案 0 :(得分:1)
您的Review
构造函数调用缺少括号:代码应为new Review().query...
而不是new Review.query...
。
作为旁注,这种编写查询的简单方法应该有效:
return new Review().where('serviceID', serviceID).fetchAll()