[英]How to call a helper function of an object?
我有一個 object ,其中有幫助函數,可以根據時間安排通知。 在用戶按下按鈕安排通知后,我在 redux 操作中調用 function。
我收到錯誤“未定義不是 object(正在評估 _'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
設為具有一個屬性的 object - 未命名的 function。 您的代碼中有太多花括號。 你想要的可能是這樣的:
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.');
}
},
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.