简体   繁体   English

undefined不是对象(评估Promise .then)

[英]undefined is not an object (evaluating Promise .then)

Im getting this error while developing react native mobile application. 我在开发本机移动应用程序时收到此错误。

fetchDB function will return a promise fetchDB函数将返回一个承诺

 fetchDB(key) {
    try {
      AsyncStorage.getItem(key, result => {
        return result;
      });
    } catch (error) {
      console.log(error);
    }
  }
}

fetching value from returned promise by using .then() 通过使用.then()从返回的承诺中获取价值

  getUserName = () => {
    var user = Storage.fetchDB("username");
    user.then(res => {
      alert(res);
    });
  };

its rendering the red screen with error 其呈现红色屏幕并显示错误

undefined is not an object 未定义不是对象

1) You need to return the promise function explicitly. 1)您需要显式返回promise函数。 Fail situation could be handled within catch callback: 失败情况可以在catch回调中处理:

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. 2)只有当AsyncStorage.getItem确实返回promise时,点(1)才AsyncStorage.getItem If it's not true, then you need to update AsyncStorage.getItem method to be promise-based. 如果不正确,则需要将AsyncStorage.getItem方法更新为基于AsyncStorage.getItem 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函数来解决此问题:

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: 3)我什至会说由于es6 Promises本质,您不需要try-catch:

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. 最后一个.catch处理从fetchDB Promise引发的任何错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 [未处理的承诺拒绝:类型错误:未定义不是对象(评估'_ref.about')] - [Unhandled promise rejection: TypeError: undefined is not an object (evaluating '_ref.about')] TypeError:“未定义”不是对象(正在评估“ promise.data.map”) - TypeError: 'undefined' is not an object (evaluating 'promise.data.map') [未处理的承诺拒绝:类型错误:未定义不是对象(评估“response.data”)] - [Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'response.data')] 未处理的 Promise 拒绝:类型错误:未定义不是 object(正在评估“data.date”) - Unhandled Promise Rejection: TypeError: undefined is not an object (evaluating 'data.date') 未定义不是对象(正在评估“ RNFSFileTypeRegular”) - undefined is not an object(evaluating 'RNFSFileTypeRegular') TypeError: undefined is not an object(评估“this”) - TypeError: undefined is not an object (evaluating 'this') Expo:未处理的 promise 拒绝:TypeError:未定义不是 object(评估“props.navigation”)] - Expo: Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'props.navigation')] 谷歌地图地理编码 API 错误 [未处理的 promise 拒绝:TypeError:undefined 不是 object(评估 'data.results[0].geometry')] - Google maps Geocoding API error [Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'data.results[0].geometry')] 可能未处理的承诺拒绝(Id:0):TypeError:undefined不是对象(评估'err.response.data') - Possible Unhandled Promise Rejection (Id: 0): TypeError: undefined is not an object (evaluating 'err.response.data') 未处理的 Promise Rejection TypeError undefined is not an object(评估服务 drugSearchService getDrugsByCriteria(criteria, val).then) - Unhandled Promise Rejection TypeError undefined is not an object (evaluating services drugSearchService getDrugsByCriteria(criteria, val).then)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM