简体   繁体   English

JavaScript Promise EsLint 部署 Firebase-Cloud-Function 时出现问题

[英]JavaScript Promise EsLint Problems when deploying Firebase-Cloud-Function

I get the following errors, when I try to deploy the following Firebase Javascript Function via the command "firebase deploy --only functions" from Firebase-Tools CLI in Version 8.6.0.当我尝试通过版本中的 Firebase-Tools CLI 中的“firebase deploy --only functions”命令部署以下 Firebase Javascript Function 时,出现以下错误。

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;
        });
    });
});

I found this example code on the official firebase documentation: https://firebase.google.com/docs/firestore/query-data/get-data#get_all_documents_in_a_collection我在官方 firebase 文档中找到了这个示例代码: https://firebase.google.com/docs/firestore/query-data/get-data#get_all_documents_in_a_collection

When I try to deploy the code there is running an esLint Check and I get the following errors:当我尝试部署代码时,正在运行 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

How I have to fix these errors?我必须如何修复这些错误? Can someone give me an example, how the Promise with Catch have to look like?谁能给我一个例子,带有 Catch 的 Promise 的外观如何?

My goal is to get the users accounts data logged in the console to do further operations later.我的目标是让用户帐户数据记录在控制台中,以便以后进行进一步的操作。 But I don't know how to get these user data logged in the console yet.但我还不知道如何让这些用户数据记录在控制台中。

If your goal is to log to the Cloud Function console the documents of the accounts collection, the following should do the trick:如果您的目标是将帐户集合的文档登录到 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;
});

Note that you MUST return a Promise or a value in a background triggered Cloud Function.请注意,您必须返回 Promise 或后台触发的 Cloud Function 中的值。 I would suggest you watch the 3 videos about "JavaScript Promises" from the Firebase video series: https://firebase.google.com/docs/functions/video-series/ which explain this key point.我建议您观看 Firebase 视频系列中关于“JavaScript Promises”的 3 个视频: https://firebase.google.com/docs/functions/video-series/解释了这个关键点。


Note also that you need to use the Admin SDK in a Cloud Function so firestore should be declared as follows:另请注意,您需要在云 Function 中使用管理员 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) => {...});

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

Try this尝试这个

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)
    });

Or create an eslintrc file and modify the rules或者创建一个eslintrc文件并修改规则

Find out about configuring Eslint here在此处了解如何配置 Eslint

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 到Firebase-Cloud-Function端点的发布请求上收到“ 500 Internal Server Error” - Receving “500 Internal Server Error” on Post Request to Firebase-Cloud-Function Endpoint 部署Firebase云function时这个错误是什么意思 - What does this error mean when deploying Firebase cloud function Firebase 云函数:事务 function 不返回 promise? - Firebase Cloud Functions: Transactions function not returning promise? Firebase 云 Function (Javascript) Firebase 部署时解析错误 - Firebase Cloud Function (Javascript) Parsing Error When Firebase Deploy 获取 `eslint' - 解析错误,同时编译 firebase 云 function - Getting `eslint' - parsing error, while compiling firebase cloud function 未定义 Cloud Firebase 函数错误承诺 - Cloud firebase function error promise is not defined Firebase 云 Function 在我尝试 forEach() “函数返回未定义,预期 Promise 或值”时给出错误 - Firebase Cloud Function gives Error when I try forEach() “Function returned undefined, expected Promise or value” " 为什么 firebase cloud-function javascript promise 运行次数超过循环调用次数? - Why does firebase cloud-function javascript promise run more than the number of loop invocations? 错误:部署谷歌云 function 时找不到模块“firebase/app” - Error: Cannot find module 'firebase/app' When deploying google cloud function 当在JavaScript中读取节点时,Firebase Cloud Function打印“未定义”吗? - Firebase Cloud Function prints 'undefined' when reading a node in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM