【发布时间】:2021-11-26 02:14:19
【问题描述】:
我正在使用 expo-calendar 并从用户的设备中检索日历对象。
收到日历后,我使用日历从各个日历中检索事件。
在请求事件时,我在记录请求的事件数据时收到{"_U":0,"_V":0,"_W":null,"_X":null}。
与此同时,我收到错误消息:[Unhandled promise rejection: Error: An exception was thrown while calling `ExpoCalendar.getEventsAsync` with arguments `(] at node_modules/react-native/Libraries/BatchedBridge/NativeModules.js:103:50 in promiseMethodWrapper at node_modules/@unimodules/react-native-adapter/build/NativeModulesProxy.native.js:15:23 in moduleName.methodInfo.name at http://127.0.0.1:19000/index.bundle?platform=ios&dev=true&hot=false&minify=false:321320:82 in getEventsAsync$ at Screens/Tasks_1.js:1018:15 in <global>
我相信这是因为请求 const events = Calendar.getEventsAsync() 正在等待承诺,但我无法确定它在等待什么响应。如何正确访问事件?
useEffect(() => {
(async () => {
//REQUEST PERMISSION TO ACCESS CALENDAR
const { status } = await Calendar.requestCalendarPermissionsAsync();
if (status === "granted") {
const calendars = await Calendar.getCalendarsAsync(
Calendar.EntityTypes.EVENT
);
//LIST OF ALL CALENDARS ON DEVICE WITH ID's
console.log("Here are all your calendars:");
console.log({ calendars });
//GET EVENTS FROM A CALENDAR ON DEVICE WITH A GIVEN ID AND TIME RANGE
const events = Calendar.getEventsAsync(
calIDs,
"2021-04-14T17:00:00.00Z",
"2021-09-14T17:00:00.00Z"
);
//THIS LOGS {"_U":0,"_V":0,"_W":null,"_X":null}
console.log("EVENTS" + JSON.stringify(events));
}
})();
}, []);
【问题讨论】:
-
不是
const events = await Calendar.getEventsAsync吗? -
你为什么照原样调用
then,而在里面什么也不做?您应该在then中使用回调的结果或使用 await,但不能同时使用。 -
那是一个错字,我把它改掉了。
标签: javascript react-native async-await expo