gpt4 book ai didi

javascript - 如何调用对象的辅助函数?

转载 作者:行者123 更新时间:2023-12-01 00:36:32 25 4
gpt4 key购买 nike

我有一个对象,其中有辅助函数,可以根据时间安排通知。在用户按下按钮来安排通知后,我在 redux 操作中调用该函数。

我收到错误“未定义不是对象(正在评估 _'ScheduleNotification.default.startReminder')”

我尝试在按下按钮时以及在操作中调用它,但两次都收到相同的错误。

我的日程通知对象 -

export const scheduleNotification = {
startReminder: {
async function(item) {
const permission = await registerForPushNotificationsAsync();
if (permission) {
Notifications.scheduleLocalNotificationAsync(
{
title: 'Reminder:',
body: `${item.text} now`
},
{
time: item.date
}
);
} else {
console.log('cannot send notification without permission.');
}
}
},
}

然后是我调用该函数的操作-

export const startReminder = (item) => {
return (dispatch) => {
dispatch({
type: START_REMINDER,
id: item.id
});
scheduleNotification.startReminder(item);
};
};

当我按下按钮时,并没有安排通知,而是出现了错误消息。如果需要,我可以提供更多代码。感谢您的帮助。

最佳答案

您已将 scheduleNotification.startReminder 创建为一个具有一个属性的对象 - 一个未命名的函数。您的代码中的花括号过多。你想要的可能是这样的:

export const scheduleNotification = {
async startReminder(item) {
const permission = await registerForPushNotificationsAsync();
if (permission) {
Notifications.scheduleLocalNotificationAsync({
title: 'Reminder:',
body: `${item.text} now`
}, {
time: item.date
});
} else {
console.log('cannot send notification without permission.');
}
},
}

关于javascript - 如何调用对象的辅助函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58085658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com