【发布时间】:2016-09-28 17:38:50
【问题描述】:
我刚开始使用 react native,目前正在为我们的项目开发一个 couchdb api。我正在用 fecht 做我的休息请求。但在内部函数中,我无法将我的响应文本(json)写入我想要返回的变量。
_getDocument(pID)
{
var result = "getDocument failed"
var document = "https://"
+ this.connection.user + ":"
+ this.connection.password + "@"
+ this.connection.server + "/"
+ this.connection.database + "/"
+ pID;
fetch(document)
.then((response) => response.text())
.then((responseText) => {
result = responseText;
Alert.alert('Fetch', result , [{text: 'ok'}])
}).catch((error) => {
console.warn(error);
});
Alert.alert('_getDocument', result , [{text: 'ok'}])
return result;
}
在我的函数结束时,变量 result 的值仍然是“getDocument failed”,而不是我的 json 字符串。 我已经尝试通过调用另一个函数将我的响应文本存储在其他地方,但效果不佳。
【问题讨论】:
标签: javascript android json react-native fetch