I am trying to update a nested collection in Firebase with my Expo React-Native app and I am receiving an error from a catch statement:
Error updating user activities map: [ReferenceError: Can't find variable: activities]
This is interesting as the function works normally and the Firestore collection still updates, however this error is logged regardless. The issue here is that my dispatch in the .then() statement is never reached as the .catch() always occurs.
I believe this is due to the firestore.collection('users').doc(userId).collection('activities').doc(activityId).set()
catch statement. I am confused because the activities collection exists and is written to in the function, and even if it does not exist, I recall reading somewhere that Firestore will automatically create the document. Can someone please provide me some assistance with this error?
DB structure: users (collection) -> [some user id] (document) -> activities (collection) -> [some activity document]
const activityRef = firestore.collection('activities').doc(activityId);
let activityInfo = {}
let participants = {}
// get activity information
activityRef.get()
.then(doc => {
activityInfo = doc.data();
participants = doc.data().participants;
})
.then(() => {
// update participant activity status
participants[userId] = 'participant';
// update activityRef with new participant
activityRef.update({ participants }).then(() => {
// add to activities collection in the current user document
firestore.collection('users').doc(userId).collection('activities').doc(activityId)
.set({
id: activityId,
status: 'participant'
})
.then(() => {
console.log('activity info', activityInfo);
dispatch({
type: 'ENROLL_ACTIVITY',
activityId,
activity: activityInfo
});
})
.catch((error) => {
console.log('catch statement: activityInfo', activityInfo);
// The document probably doesn't exist.
console.log("Error updating user activities map: ", error);
})
})
.catch(error => {
// The document probably doesn't exist.
console.error("Error updating activity document: ", error);
});
})
Thank you for reading this! Any information would be helpful in trying to resolve this error :)
EDIT: If I remove the catch statement
.catch((error) => {
console.log('catch statement: activityInfo', activityInfo);
// The document probably doesn't exist.
console.log("Error updating user activities map: ", error);
})
then I get this yellow warning unhandled promise rejection, does this error look familiar to anyone?
[Unhandled promise rejection: ReferenceError: Can't find variable: activities]
Stack trace:
node_modules/immer/dist/immer.module.js:39:2 in assignMap
node_modules/redux/lib/redux.js:464:32 in combination
node_modules/redux/lib/redux.js:218:6 in dispatch
node_modules/promise/setimmediate/core.js:37:13 in tryCallOne
node_modules/promise/setimmediate/core.js:123:24 in setImmediate$argument_0
node_modules/react-native/Libraries/Core/Timers/JSTimers.js:135:14 in _callTimer
node_modules/react-native/Libraries/Core/Timers/JSTimers.js:183:16 in _callImmediatesPass
node_modules/react-native/Libraries/Core/Timers/JSTimers.js:446:30 in callImmediates
[native code]:null in callImmediates
node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:396:6 in __callImmediates
node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:144:6 in __guard$argument_0
node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:373:10 in __guard
node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:143:4 in flushedQueue
[native code]:null in flushedQueue
[native code]:null in callFunctionReturnFlushedQueue
...