我是loopback和node.js的新手。
我创建了两个模型:Rating和RatingsAggregate 使用loopback资源管理器,我可以查询和发布API就好了。
我尝试设置一些基本的业务逻辑,所以我在共同/模型中编辑Rating.js文件 以下是它的内容:
module.exports = function(Rating) {
Rating.afterRemote('**', function(ctx, inst, next) {
var loopback = require('loopback');
var app = loopback();
var ratingsaggregate = app.models.ratingsaggregate;
ratingsaggregate.post({"source":"foobar","restaurantID":"foobar","itemMenuName":"foobar","itemSectionName":"foobar","itemName":"foobar","nRatings1":123,"nRatings2":123,"nRatings3":123,"nRatings4":123,"nRatings5":123,"hasImage":true,"imageSize":123,"latestImageRatingID":"foobar","imageCount":123,"lastUpdated":"foobar"}, function(err, response) {
if (err) console.error(err);
next();
});
});
};
我可以加载我的API,但每当我对它运行一个get语句时,我都会收到此错误:
TypeError: Cannot call method 'post' of undefined
我的猜测是,以某种方式收视率从未获得价值......但我不知道我做错了什么。显然这不是我的业务逻辑的最终状态,但我正在尝试两个模型之间的一些基本CRUD
答案 0 :(得分:3)
而且......这就是答案。文档中隐藏了一个getModel函数
module.exports = function(Rating) {
Rating.afterRemote('create', function(ctx, inst, next) {
var loopback = require('loopback');
var ratingsaggregate = loopback.getModel('ratingsaggregate');
ratingsaggregate.create({"source":"foobar","restaurantID":"foobar","itemMenuName":"foobar","itemSectionName":"foobar","itemName":"foobar","nRatings1":123,"nRatings2":123,"nRatings3":123,"nRatings4":123,"nRatings5":123,"hasImage":true,"imageSize":123,"latestImageRatingID":"foobar","imageCount":123,"lastUpdated":"foobar"}, function(err, response) {
if (err) console.error(err);
next();
});
});
};
修复所有内容,行为是预期的行为