【问题标题】:How to call a helper function of an object?如何调用对象的辅助函数?
【发布时间】:2019-09-24 17:43:07
【问题描述】:

我有一个对象,其中我有帮助函数,可以根据时间安排通知。在用户按下按钮安排通知后,我在 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);
  };
};

在我按下按钮的那一刻,错误消息出现了,而不是安排通知。如果需要,我可以提供更多代码。感谢您的帮助。

【问题讨论】:

    标签: javascript react-native redux expo


    【解决方案1】:

    您已经使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.');
        }
      },
    }
    

    【讨论】:

    • 我把它改成了那个,但这并没有解决问题。我仍然收到未定义的不是对象错误。我是否必须改变调用函数的方式?
    • @goolius_boozler 你如何导入scheduleNotification
    • 啊,原来如此。我把花括号放在它周围,效果很好。谢谢。
    猜你喜欢
    • 2017-03-27
    • 1970-01-01
    • 2018-04-28
    • 2011-06-24
    • 1970-01-01
    • 2015-05-08
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多