无法理解Cloud Function错误:Node.js/TypeScript新手求助
Hey there! I totally get how frustrating this error can be when you're new to Node.js/TypeScript Cloud Functions—promises can feel tricky at first. Let's break down exactly why this is happening and fix your code.
Why the error happens
Cloud Functions requires your function to return either a value (for synchronous work) or a Promise (for asynchronous work). This tells the runtime when your function has finished executing. Right now, your code is doing neither in some paths:
- In the
ifblock, you start an asynchronous operation withquery.get().then(...)but don't return that Promise chain. The function moves on and returns undefined before the async work finishes. - In the
elseblock, you just doreturnwith no value, which also returns undefined.
On top of that, I spotted a small typo that would break your batch update: you're targeting a collection called compID instead of compRequests (since you queried compRequests earlier). That would mean you're trying to update documents in the wrong collection—definitely something to fix!
Fixed code
Here's the corrected version with explanations of the changes:
exports.compReqUpdated = functions.firestore .document('/compRequests/{id}') .onUpdate((change, context) => { // Fixed typo: contex → context const newData = change.after.data(); const dbConst = admin.firestore(); const reqStatus: string = newData.requestStatus; const compId: string = newData.compID; const reqActive: boolean = newData.requestActive; if (reqStatus == "CANCELED" && reqActive) { const query = dbConst.collection('compRequests').where('compID', '==', compId); const batch = dbConst.batch(); // Return the entire Promise chain so Cloud Functions waits for it to complete return query.get() .then(querySnapshot => { const docs = querySnapshot.docs; for (const doc of docs) { console.log(`Document found at path: ${doc.ref.path}`); console.log(doc.id); // Fixed collection name: compID → compRequests const docRef = dbConst.collection('compRequests').doc(doc.id); batch.update(docRef, { requestStatus: 'CANCELED', requestActive: false }); }; return batch.commit(); // Return the batch commit Promise }) .catch(error => { console.error('Error updating documents:', error); // Re-throw the error so Cloud Functions knows something went wrong throw error; }); } else { // Return a resolved Promise or a value to avoid undefined return Promise.resolve(); } });
Key changes made:
- Returned the Promise chain: In the
ifblock, we now returnquery.get().then(...)so Cloud Functions waits for the asynchronous query and batch commit to finish. - Fixed collection typo: Changed
compIDtocompRequestswhen creating the document reference for the batch update. - Proper error handling: Added
throw errorin the catch block so Cloud Functions reports the error instead of silently ignoring it. - Valid return in else block: Replaced empty
returnwithreturn Promise.resolve()to ensure we don't return undefined. - Fixed typo: Changed
contextocontextin the function parameters (a small syntax fix that could cause issues).
With these changes, your function will properly signal when it's done executing, and the error should disappear. Let me know if you run into any other issues!
内容的提问来源于stack exchange,提问作者joffd

