gpt4 book ai didi

javascript - 环回 : cannot call method 'post' of undefined

转载 作者:行者123 更新时间:2023-11-28 19:36:12 24 4
gpt4 key购买 nike

我是 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 语句时,都会收到此错误:

类型错误:无法调用未定义的“post”方法

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

最佳答案

而且......这就是答案。文档中隐藏了一个 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();
});

});

};

修复了所有问题,并且行为符合预期

关于javascript - 环回 : cannot call method 'post' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25840264/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com