Node.js Firebase部署报错求助:ESLint检测不通过问题
Fixing ESLint Errors in Your Firebase Cloud Function
Hey there! I see you've been stuck on these ESLint errors for hours—let's get this sorted out. First, let's break down what each error means, then fix your code step by step.
Understanding the Errors
promise/no-nestingwarnings: ESLint is flagging nested.then()calls because they make code harder to read and debug. We can fix this by flattening the promise chain or usingasync/await(a cleaner approach for beginners!).promise/always-returnerror: Your final.then()callback doesn’t return a value or throw an error. Promises rely on returning values to keep the chain intact, so we need to ensure every asynchronous step returns something.
Fixed Code
Let’s rewrite your function using async/await—this eliminates nested promises entirely and fixes all ESLint issues:
'use strict' const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(functions.config().firebase); exports.sendNotification = functions.database.ref('/notifications/{user_id}/{notification_id}').onWrite(async (event) => { const user_id = event.params.user_id; const notification_id = event.params.notification_id; console.log('We have a notification from : ', user_id); // Exit early if the notification was deleted if (!event.data.val()) { console.log('A Notification has been deleted from the database : ', notification_id); return null; // Return a value to properly resolve the function's promise } // Fetch the user who sent the notification const fromUserResult = await admin.database().ref(`/notifications/${user_id}/${notification_id}`).once('value'); const from_user_id = fromUserResult.val().from; console.log('You have new notification from : ', from_user_id); // Fetch username and device token in parallel for efficiency const [userQueryResult, deviceTokenResult] = await Promise.all([ admin.database().ref(`Users/${from_user_id}/name`).once('value'), admin.database().ref(`/Users/${user_id}/device_token`).once('value') ]); const userName = userQueryResult.val(); const token_id = deviceTokenResult.val(); // Build the notification payload const payload = { notification: { title: "New Friend Request", body: `${userName} has sent you request`, icon: "default", click_action: "in.tvac.akshaye.lapitchat_TARGET_NOTIFICATION" }, data: { from_user_id: from_user_id } }; // Send the notification and return the response const response = await admin.messaging().sendToDevice(token_id, payload); console.log('This was the notification Feature'); return response; // Return the response to fix the always-return error });
Key Changes Explained
- Switched to
async/await: This replaces nested.then()calls with linear, easy-to-follow code, eliminating thepromise/no-nestingwarnings. Theasynckeyword marks the function as asynchronous, andawaitlets us wait for promises to resolve without nesting. - Fixed the
promise/always-returnerror: We now return explicit values at every code path—nullwhen exiting early, and the response fromsendToDeviceat the end. This ensures ESLint’s rule is satisfied and the promise chain stays intact. - Cleaner parallel queries: We kept
Promise.all()but used array destructuring to assign results directly to variables, making the code more readable than usingresult[0]orresult[1]. - Explicit early exit: Returning
nullwhen the notification is deleted ensures the function doesn’t hang and properly resolves its promise.
Give this code a try—your ESLint errors should disappear, and your notification function will work as expected!
内容的提问来源于stack exchange,提问作者lllllllllllllll
相关产品推荐
相关产品推荐

