【问题标题】:JavaScript Promise EsLint Problems when deploying Firebase-Cloud-Function部署 Firebase-Cloud-Function 时的 JavaScript Promise EsLint 问题
【发布时间】:2020-11-13 08:47:30
【问题描述】:

当我尝试通过版本 8.6.0 中 Firebase-Tools CLI 中的“firebase deploy --only functions”命令部署以下 Firebase Javascript 函数时,出现以下错误。

exports.notifyNewMessage = functions.firestore.document("posts/{postId}").onCreate((docSnapshot, context) => {
    firestore.collection('accounts').get().then(function (querySnapshot) {
        querySnapshot.forEach(function (doc) {
            // doc.data() is never undefined for query doc snapshots
            console.log(doc.id, " => ", doc.data());
            return  doc.data().name;
        });
    });
});

我在官方 firebase 文档中找到了这个示例代码:https://firebase.google.com/docs/firestore/query-data/get-data#get_all_documents_in_a_collection

当我尝试部署代码时,正在运行 esLint Check,我收到以下错误:

 2:5   error    Expected catch() or return                  promise/catch-or-return
 2:49  warning  Unexpected function expression              prefer-arrow-callback
 2:49  error    Each then() should return a value or throw  promise/always-return
 3:31  warning  Unexpected function expression              prefer-arrow-callback

我必须如何修复这些错误? 谁能给我一个例子,Catch 的 Promise 应该是什么样子?

我的目标是让用户帐户数据记录在控制台中,以便以后进行进一步的操作。但我还不知道如何让这些用户数据记录在控制台中。

【问题讨论】:

  • 您的云函数的确切目标是什么?您需要正确返回异步方法返回的 Promises 链。
  • 不清楚您希望我们如何回答这个问题。您想禁用 linting(可能不会)还是想知道如何在 Promise 上使用 .catch()
  • 如果您单独搜索每条消息,您将找到有关其含义以及如何更正它的信息。每条消息还告诉您它所指的代码行。

标签: javascript firebase promise google-cloud-functions


【解决方案1】:

如果您的目标是将帐户集合的文档记录到 Cloud Function 控制台,则以下内容应该可以解决问题:

exports.notifyNewMessage = functions.firestore.document("posts/{postId}").onCreate((docSnapshot, context) => {
    return firestore.collection('accounts').get()   // We return the promise chain, see the video series mentioned below
    .then(querySnapshot => {    //  Unexpected function expression 
        querySnapshot.forEach(doc => {   // Unexpected function expression 
            // doc.data() is never undefined for query doc snapshots
            console.log(doc.id, " => ", doc.data());
        });
        return null;    // Each then() should return a value or throw 
    })
    .catch(error => {
        console.log(error.message);
        return null;
});

请注意,您必须在后台触发的云函数中返回 Promise 或值。我建议您观看 Firebase 视频系列中关于“JavaScript Promises”的 3 个视频:https://firebase.google.com/docs/functions/video-series/,其中解释了这一关键点。


另请注意,您需要在 Cloud Function 中使用 Admin SDK,因此 firestore 应声明如下:

// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');

// The Firebase Admin SDK to access Cloud Firestore.
const admin = require('firebase-admin');
admin.initializeApp();

const firestore = admin.firestore();


exports.notifyNewMessage = functions.firestore
    .document("posts/{postId}")
    .onCreate((docSnapshot, context) => {...});

参考:https://firebase.google.com/docs/functions/get-started

【讨论】:

    【解决方案2】:

    试试这个

    exports.notifyNewMessage = functions.firestore
        .document("posts/{postId}")
        .onCreate((docSnapshot, context) => {
            firestore.collection('accounts')
                .get()
                .then((querySnapshot) => {
                    return querySnapshot.forEach((doc) => {
                        console.log(doc.id, " => ", doc.data());
                        return  doc.data().name;
                    });
                })
                .catch((err) => err)
        });
    

    或者创建一个eslintrc文件并修改规则

    了解如何配置 Eslint here

    【讨论】:

      猜你喜欢
      • 2020-04-29
      • 2023-03-13
      • 2021-01-09
      • 2018-11-27
      • 1970-01-01
      • 2019-10-25
      • 2022-12-12
      • 2021-05-14
      • 2020-10-19
      相关资源
      最近更新 更多