![](/img/trans.png)
[英]Error: Could not load the default credentials (Firebase function to firestore)
[英]Error: Could not load the default credentials. - Cloud Functions
我正在为我的 react-native 应用开发群组功能。 并且我希望向创建组时添加的用户发送云消息。 我正在使用云功能来做到这一点。
但是我在我的函数中遇到了这个错误:
Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
at GoogleAuth.getApplicationDefaultAsync (/srv/node_modules/google-auth-library/build/src/auth/googleauth.js:161:19)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:229:7)
它无法从 firestore 获取 fcm-token 来发送通知。
我已经编写了用于发送好友请求的云函数,并且在其中,从云 firestore 成功检索了令牌,并发送了通知。
这是我的云功能:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
//======================NOTIFY ADDED MEMBERS==========================//
exports.notifyAddedMembers = functions.https.onCall((data, context) => {
const members = data.members;
const groupName = data.groupName;
var tokens = [];
members.forEach(async member => {
//send notifications to member.uid
console.log('MEMBER.UID ', member.uid);
await fetchTokenFromUid(member.uid)
.then(token => {
console.log('retrieved token: ', token);
// tokens.push(token);
const payload = {
notification: {
title: `You have been added to ${groupName}`,
body: 'Share your tasks',
sound: 'default',
},
};
return admin.messaging().sendToDevice(token, payload);
})
.catch(err => console.log('err getting token', err));
});
// console.log('ALL TOKENS: ', tokens);
console.log('GROUP NAME: ', groupName);
});
async function fetchTokenFromUid(uid) {
var token = '';
return await admin
.firestore()
.collection('Users')
.doc(`${uid}`)
.get()
.then(async doc => {
console.log('uid token: ', Object.keys(doc.data().fcmTokens));
var tokenArray = Object.keys(doc.data().fcmTokens); //ARRAY
for (var i = 0; i < tokenArray.length; i++) {
token = tokenArray[i]; //Coverts array to string
}
return token; //return token as string
});
}
我正在使用 react-native-firebase 库。
您正在正确加载firebase-functions
和firebase-admin
模块,并初始化admin
应用程序实例。
我不知道究竟是什么导致了你得到的错误,但基于这个SO 问题,这可能是因为在你的 Cloud Function 中,你将async/await
的使用与then()
方法混合使用。
您的index.js
文件中是否还有其他 Cloud Function? 特别是一些与其他 Google API 交互的 API。
我建议使用Promise.all()
如下重构您的代码。 您首先获取所有令牌,然后发送消息。
exports.notifyAddedMembers = functions.https.onCall(async (data, context) => {
try {
const members = data.members;
const groupName = data.groupName;
const promises = [];
members.forEach(member => {
promises.push(admin
.firestore()
.collection('Users')
.doc(member.uid).get());
});
const tokensSnapshotsArray = await Promise.all(promises);
const promises1 = [];
tokensSnapshotsArray.forEach(snap => {
const token = snap.data().fcmToken; //Here you may adapt as it seems you have an array of tokens. I let you write the loop, etc.
const payload = {
notification: {
title: `You have been added to ${groupName}`,
body: 'Share your tasks',
sound: 'default',
},
};
promises1.push(admin.messaging().sendToDevice(token, payload));
});
await Promise.all(promises1);
return { result: 'OK' }
} catch (error) {
//See the doc: https://firebase.google.com/docs/functions/callable#handle_errors
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.