0

我是 loopback 和 node.js 的新手。

我创建了两个模型:Rating 和 RatingsAggregate 使用环回资源管理器,我可以很好地查询和发布 API。

我正在尝试设置一些基本的业务逻辑,所以我在 common/models 中编辑文件 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

我的猜测是,ratingaggregate 永远不会获得价值......但我不知道我做错了什么。显然这不是我的业务逻辑的最终状态,但我现在正在两个模型之间尝试一些基本的 CRUD

4

1 回答 1

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

  });

};

修复一切,行为是预期的

于 2014-09-15T03:34:20.413 回答