我是ES6的新手,反应原生,试图从SecureStore中获取多个值。
我认为我在这里误解了承诺... global.userData在Promise.all(promises).then函数中为空。相关值确实存在于安全存储
中我的代码是: -
getUserData(fields) {
var promises = [];
var that = this;
global.userData = {};
function getField(field) {
return SecureStore.getItemAsync(field)
.then(res => {
console.log(field+"="+res); // this appears after the log below
global.userData[field] = res;
})
.catch(error => {
global.userData[field] = null;
});
}
fields.map(field => {
promises.push[getField(field)];
});
Promise.all(promises).then(function(v) {
console.log(global.userData); // this is empty
that.setState({ isReady: true }); // allow page to render
});
}
getUserData(["userId", "userName","etc"]);
答案 0 :(得分:1)
我的不好......无意中使用了
promises.push[getField(field)];
应该是:
promises.push(getField(field));
惊讶它没有被检测为语法错误......