admin管理员组文章数量:1025251
Im getting this error while developing react native mobile application.
fetchDB
function will return a promise
fetchDB(key) {
try {
AsyncStorage.getItem(key, result => {
return result;
});
} catch (error) {
console.log(error);
}
}
}
fetching value from returned promise by using .then()
getUserName = () => {
var user = Storage.fetchDB("username");
user.then(res => {
alert(res);
});
};
its rendering the red screen with error
undefined is not an object
Im getting this error while developing react native mobile application.
fetchDB
function will return a promise
fetchDB(key) {
try {
AsyncStorage.getItem(key, result => {
return result;
});
} catch (error) {
console.log(error);
}
}
}
fetching value from returned promise by using .then()
getUserName = () => {
var user = Storage.fetchDB("username");
user.then(res => {
alert(res);
});
};
its rendering the red screen with error
Share Improve this question edited Oct 5, 2017 at 11:54 dhilt 20.9k8 gold badges79 silver badges93 bronze badges asked Oct 5, 2017 at 9:57 KartiikeyaKartiikeya 2,5269 gold badges39 silver badges70 bronze badges 1undefined is not an object
-
AsyncStorage.getItem
is asynchronous? anyway, a return value inside a callback function doesn't mean you're returning something fromfetchDB
function ... so you have two issues ... understanding asynchronous code, and understanding that your function doesn't actually return anything anyway ... try simplyreturn AsyncStorage.getItem(key);
– Jaromanda X Commented Oct 5, 2017 at 10:04
1 Answer
Reset to default 41) You need to return the promise function explicitly. Fail situation could be handled within catch callback:
fetchDB(key) {
return AsyncStorage.getItem(key).catch(error => console.log(error));
}
fetchDB("username").then(res => alert(res));
2) Point (1) works only if AsyncStorage.getItem
really returns promise. If it's not true, then you need to update AsyncStorage.getItem
method to be promise-based. If you have not access to that method or don't want to touch it, the issue could be done also via updating fetchDB
function in a way like:
fetchDB(key) {
return new Promise((resolve, reject) => {
try {
AsyncStorage.getItem(key, resolve);
}
catch (error) {
reject(error);
}
})
}
fetchDB("username")
.then(res => alert(res))
.catch(error => console.log(error));
3) I would even say that you don't need try-catch due to es6 Promises nature:
fetchDB(key) {
return new Promise((resolve, reject) =>
AsyncStorage.getItem(key, resolve)
)
}
fetchDB("username")
.then(res => alert(res))
.catch(error => console.log(error));
That last .catch
handles any errors thrown from fetchDB
Promise.
Im getting this error while developing react native mobile application.
fetchDB
function will return a promise
fetchDB(key) {
try {
AsyncStorage.getItem(key, result => {
return result;
});
} catch (error) {
console.log(error);
}
}
}
fetching value from returned promise by using .then()
getUserName = () => {
var user = Storage.fetchDB("username");
user.then(res => {
alert(res);
});
};
its rendering the red screen with error
undefined is not an object
Im getting this error while developing react native mobile application.
fetchDB
function will return a promise
fetchDB(key) {
try {
AsyncStorage.getItem(key, result => {
return result;
});
} catch (error) {
console.log(error);
}
}
}
fetching value from returned promise by using .then()
getUserName = () => {
var user = Storage.fetchDB("username");
user.then(res => {
alert(res);
});
};
its rendering the red screen with error
Share Improve this question edited Oct 5, 2017 at 11:54 dhilt 20.9k8 gold badges79 silver badges93 bronze badges asked Oct 5, 2017 at 9:57 KartiikeyaKartiikeya 2,5269 gold badges39 silver badges70 bronze badges 1undefined is not an object
-
AsyncStorage.getItem
is asynchronous? anyway, a return value inside a callback function doesn't mean you're returning something fromfetchDB
function ... so you have two issues ... understanding asynchronous code, and understanding that your function doesn't actually return anything anyway ... try simplyreturn AsyncStorage.getItem(key);
– Jaromanda X Commented Oct 5, 2017 at 10:04
1 Answer
Reset to default 41) You need to return the promise function explicitly. Fail situation could be handled within catch callback:
fetchDB(key) {
return AsyncStorage.getItem(key).catch(error => console.log(error));
}
fetchDB("username").then(res => alert(res));
2) Point (1) works only if AsyncStorage.getItem
really returns promise. If it's not true, then you need to update AsyncStorage.getItem
method to be promise-based. If you have not access to that method or don't want to touch it, the issue could be done also via updating fetchDB
function in a way like:
fetchDB(key) {
return new Promise((resolve, reject) => {
try {
AsyncStorage.getItem(key, resolve);
}
catch (error) {
reject(error);
}
})
}
fetchDB("username")
.then(res => alert(res))
.catch(error => console.log(error));
3) I would even say that you don't need try-catch due to es6 Promises nature:
fetchDB(key) {
return new Promise((resolve, reject) =>
AsyncStorage.getItem(key, resolve)
)
}
fetchDB("username")
.then(res => alert(res))
.catch(error => console.log(error));
That last .catch
handles any errors thrown from fetchDB
Promise.
本文标签: javascriptundefined is not an object (evaluating Promise then)Stack Overflow
版权声明:本文标题:javascript - undefined is not an object (evaluating Promise .then) - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745615308a2159253.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论