【发布时间】:2016-10-26 18:10:22
【问题描述】:
我正在开发 facebook bot,但我绝不是 node.js 开发人员,这是我第一次使用它,因为我想稍微走出自己的舒适区。
这是我的请求函数
function requestExc() {
var resDictionary = {}
unirest.get("http://openapi.ro/api/exchange/" + queryDict["code"] + ".json")
.query({"date" : queryDict["date"]})
.end(function(res) {
if (res.error) {
console.log('GET error', res.error)
} else {
console.log('GET response', res.body)
resDictionary["rate"] = res.body["rate"]
resDictionary["date"] = res.body["date"]
}
})
console.log("resDictionary IS " + resDictionary)
///prints resDictionary IS [object Object]
return resDictionary
}
所以我想得到它的结果
var response = requestExc()
if (response !== null) {
respondToSender(response, sender)
}
然后采取相应的行动
function respondToSender(res, sender) {
console.log("RES IS " + res)
//prints RES IS [object Object]
if (res["rate"] === null) {
//do stuff
}
}
但是当变量到达 respondToSender 时,它始终是未定义的。
TypeError: Cannot read property 'rate' of undefined
我也尝试过使用 Json.parse() 但它是同一件事。
【问题讨论】:
-
一方面,您可能应该使用
if (response)而不是if (response !== null);另一方面,这并不能解释为什么它总是未定义。 -
谢谢,我会更新的!
标签: javascript node.js response unirest