我正在为Google的对话框聊天机器人在Firebase中编写这个实现。
我正在尝试获取Count的值,但它显示为null。
下面是API响应:
{“计数”:1385}
这是我的密码:
function getCount(cloudFnResponse) {
var pathString = "//someApiPath";
console.log('Path string: ' + pathString);
var request = https.get({
//method:"GET",
host: "//someApiHost",
path: pathString
}, function(response) {
var json = "";
console.log("Log1=> response is: " + response);
response.on('data', function(chunk) {
console.log("log2=> Received json response: " + chunk);
json += chunk;
});
response.on('end', function() {
var jsonData = JSON.parse(json);
console.log("log3=> jsonData is: " + jsonData);
var count = jsonData[0].Count;
console.log("log4=> count is: " + JSON.stringify(count));
var chat = "Count is " + count;
console.log("log5=> chat is: " + chat);
cloudFnResponse.send(buildChatResponse(chat));
});
});
}
我添加了用于调试的日志,下面是上面代码的输出日志:
log1=> response is: [object Object]
log2=> Received json response: [{"Count":null}]
log3=> jsonData is: [object Object]
log4=> bot count is: undefined
log5=> chat is: Count is undefined
我还在想,也许它与API响应有关,整数部分: 1385,不是用双引号括起来的吗?
对于如何成功地获得整数值,有什么建议吗?它一直转到零。
发布于 2018-06-29 01:47:12
根据API响应,更新函数如下-
您应该使用Count
而不是count
,因为它返回为Count
function getCount(cloudFnResponse) {
var pathString = "//someApiPath";
console.log('Path string: ' + pathString);
var request = https.get({
//method:"GET",
host: "//someApiHost",
path: pathString
}, function(response) {
var json = "";
console.log("Log1=> response is: " + response);
response.on('data', function(chunk) {
console.log("log2=> Received json response: " + chunk);
json += chunk;
});
response.on('end', function() {
var jsonData = JSON.parse(json);
console.log("log3=> jsonData is: " + jsonData);
var Count = jsonData[0].Count;
console.log("log4=> count is: " + JSON.stringify(Count));
var chat = "Count is " + Count;
console.log("log5=> chat is: " + chat);
cloudFnResponse.send(buildChatResponse(chat));
});
});
}
https://stackoverflow.com/questions/51098833
复制