JavaScript承诺在部署Firebase云函数时会出现EsLint问题
当我试图通过8.6.0版Firebase Tools CLI中的命令“Firebase deploy--only functions”部署以下Firebase Javascript函数时,出现以下错误JavaScript承诺在部署Firebase云函数时会出现EsLint问题,javascript,firebase,promise,google-cloud-functions,Javascript,Firebase,Promise,Google Cloud Functions,当我试图通过8.6.0版Firebase Tools CLI中的命令“Firebase deploy--only functions”部署以下Firebase Javascript函数时,出现以下错误 exports.notifyNewMessage = functions.firestore.document("posts/{postId}").onCreate((docSnapshot, context) => { firestore.collection(
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官方文档中找到了以下示例代码:
当我尝试部署代码时,正在运行esLint检查,我得到以下错误:
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的承诺是什么样子的
我的目标是让用户帐户数据记录在控制台中,以便以后执行进一步的操作。但是我还不知道如何将这些用户数据记录到控制台中。如果您的目标是将帐户集合的文档记录到云功能控制台中,那么下面应该可以做到这一点:
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;
});
请注意,您必须在后台触发的云函数中返回承诺或值。我建议您观看Firebase视频系列中关于“JavaScript承诺”的3个视频:这可以解释这一关键点
还请注意,您需要在云函数中使用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) => {...});
参考资料:试试这个
exports.notifyNewMessage=functions.firestore
.document(“posts/{postId}”)
.onCreate((docSnapshot,上下文)=>{
firestore.collection('帐户')
.get()
.然后((querySnapshot)=>{
返回querySnapshot.forEach((doc)=>{
console.log(doc.id,=>,doc.data());
返回单据数据()名称;
});
})
.catch((err)=>err)
});
或者创建一个eslintrc
文件并修改规则
了解有关配置Eslint的信息云功能的确切目标是什么?您需要正确地返回异步方法返回的承诺链。不清楚您希望我们如何回答这个问题。您想禁用linting(可能不禁用),还是想知道如何在
Promise
上使用.catch()
?如果您单独搜索每条消息,您将找到有关其含义以及如何更正的信息。每条消息还告诉您它所指的代码行。