You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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-nesting warnings: 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 using async/await (a cleaner approach for beginners!).
  • promise/always-return error: 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

  1. Switched to async/await: This replaces nested .then() calls with linear, easy-to-follow code, eliminating the promise/no-nesting warnings. The async keyword marks the function as asynchronous, and await lets us wait for promises to resolve without nesting.
  2. Fixed the promise/always-return error: We now return explicit values at every code path—null when exiting early, and the response from sendToDevice at the end. This ensures ESLint’s rule is satisfied and the promise chain stays intact.
  3. Cleaner parallel queries: We kept Promise.all() but used array destructuring to assign results directly to variables, making the code more readable than using result[0] or result[1].
  4. Explicit early exit: Returning null when 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:13:15