node.js - 进程崩溃:TypeError:回调不是函数
问题描述
我正在尝试调用具有 HTTP 发布请求的此函数,以便我可以在更大脚本的不同部分获取正文
下面是代码:
var request = require('request')
var myJSON = require("JSON");
function getJSON ( input, callback){
var all = {
'documents': [
{
'id': '1',
// Change this text to test
'text': 'not helpful'
}
]
};
request({
headers: {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key' :'0df563b09d8b42b095dd32158e4afd13',
'Host' : 'westus.api.cognitive.microsoft.com'
},
uri: 'https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment',
json: true,
body: all,
method: 'POST'
}, function (error, response, body) {
if (error || response.statusCode !== 200) {
callback(error || {statusCode: response.statusCode});
}
else
callback(body);
return callback;
});
}
body = getJSON("test");
解决方案
在您的代码片段中,当您调用函数 getJSON 时,您没有提供回调参数。
body = getJSON("test"); // <--- Missing parameter
意思是,getJSON("test") 缺少回调参数。IE
getJSON("test", function(){console.log("Do something")});
这可能会导致回调参数未定义。
推荐阅读
- csv - pyspark 将模式应用于 csv - 仅返回空值
- java - Apache Karaf 以错误开头 - protocol_version
- javascript - 如何在表单提交完成之前检测是否已按下停止加载页面按钮
- html - 如何在 Angular 中从一个组件路由到另一个组件的一部分?
- .net - 将对象列表反序列化为另一个集合类型
- mongodb - 如何在 MongoDB 函数中设置 http 会话 cookie?
- javascript - 使用 ~ 符号导入/导出
- javascript - 根据时间更改行的颜色
- c++ - 链接器错误/在 C++ 中创建可包含在多个文件中的“帮助器”类的最佳方法
- java - 通过唯一名称查找实体,如果未找到则创建,填充然后保存。由于并发,两个事务节省了两倍