TypeError:未定义不是一个对象(评估'details.date.getTime')

Posted

技术标签:

【中文标题】TypeError:未定义不是一个对象(评估\'details.date.getTime\')【英文标题】:TypeError: undefined is not an object (evaluating 'details.date.getTime')TypeError:未定义不是一个对象(评估'details.date.getTime') 【发布时间】:2020-09-23 12:41:24 【问题描述】:

我使用https://github.com/zo0r/react-native-push-notification 来显示通知,在我的情况下,我使用带有日期时间选择器的预定通知。我之前在另一个项目中使用过它,它工作正常,没有错误,但在这个项目中我得到 TypeError: undefined is not an object (evalating 'details.date.getTime') 在我按下确认后出现此错误在设置时间等之后。

notifSchedule = (todo, date) => 
PushNotification.localNotificationSchedule(
  id: todo.id,
  message: todo.title,
  date,
  allowWhileIdle: true,
  priority: 'high',
);
;

这正是我在另一个项目中所做的,所以它应该可以工作。唯一不同的是我现在使用类组件,而在另一个项目中只是函数。

当我使用 date: new Date(Date.now() + 60 * 1000), // in 60 secs 而不是 date 时,它​​甚至不会触发任何事情。

编辑2: 完整的错误信息

Possible Unhandled Promise Rejection (id: 3):
TypeError: undefined is not an object (evaluating 'details.date.getTime')
localNotificationSchedule@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:129002:38
notifSchedule
handleChange@http://10.0./index.bundle?platform=android&dev=true&minify=false:129538:16
resolve@http://10.81/index.bundle?platform=android&dev=true&minify=false:129662:19
tryCallOne@http://181/index.bundle?platform=android&dev=true&minify=false:27056:16
http://10.81/index.bundle?platform=android&dev=true&minify=false:27157:27
_callTimer@http://881/index.bundle?platform=android&dev=true&minify=false:30596:17
_callImmediatesPass@http://10.081/index.bundle?platform=android&dev=true&minify=false:30635:17
callImmediates@http://1081/index.bundle?platform=android&dev=true&minify=false:30852:33
__callImmediates@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:2736:35
http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:2522:34
__guard@http://1801/index.bundle?platform=android&dev=true&minify=false:2719:15
flushedQueue@http://1081/index.bundle?platform=android&dev=true&minify=false:2521:21
flushedQueue@[native code]
invokeCallbackAndReturnFlushedQueue@[native code]

【问题讨论】:

我想我可以是 `date: new Date(Date.now() + 60 * 1000), // in 60 secs`,你能测试一下吗? 【参考方案1】:

传入的date 参数可能未定义。调试确切传递给localNotificationSchedule 的内容,这可能是您在组件生命周期中犯的一些错误引起的

【讨论】:

没有日期没有被低估。将鼠标悬停在它上面时,它会显示(参数)日期:任意。当检查具有功能组件的另一个项目时,它说的相同,但它适用于该项目【参考方案2】:

确保您的日期按照函数的要求进行格式化,然后使用:date: date

notifSchedule = (todo, date) => 
PushNotification.localNotificationSchedule(
  id: todo.id,
  message: todo.title,
  date: date,
  allowWhileIdle: true,
  priority: 'high',
);
;

【讨论】:

【参考方案3】:

正如您所说,您使用的是基于类的组件,所以我想补充一点,请确保您正确绑定了函数,否则它将无法工作。基于类的函数需要绑定函数,而函数是自己完成的。有时值可能似乎未定义,因为您没有正确接近它们。 调试您从中提取日期的详细信息或这样做

console.log(details); //if undefined do some action to fill it

details && details.date.getTime; //for conditional rendering 

【讨论】:

“基于类的函数需要绑定函数,而函数自己完成”这很可能是它不起作用的原因,我没有任何绑定,也没有任何与日期有关的状态 @iLightFPS 您必须提供代码的额外细节才能查明真正的问题。因为这里我们只需要查看你提供的功能。 更新了代码,我的state = 只有todoId和newTodo init 调试您从中提取日期的详细信息或执行以下操作:“details && details.date.getTime” 用完整的错误信息再次更新了代码。现在没有什么对我有用,尝试了大多数事情都没有成功【参考方案4】:

我自己修复了它(待办事项,日期),方法是删除待办事项并且只有(日期)有效,

【讨论】:

以上是关于TypeError:未定义不是一个对象(评估'details.date.getTime')的主要内容,如果未能解决你的问题,请参考以下文章

TypeError:未定义不是对象(评估'_ref.state)

TypeError:未定义不是对象(评估'this.setState')[重复]

TypeError:未定义不是对象(评估“window.location.href”)

TypeError:未定义不是对象(评估'array.length')反应原生[关闭]

TypeError:未定义不是对象(评估“appState.remove”)

如何在本机反应中修复“TypeError:未定义不是对象(评估'_reactNativeCamera.default.constants')”错误?