我将实时数据库更改为Firestore。 使用云功能触发onUpdate()始终失败并显示错误消息。
这是我的云功能 index.js
const admin = require("firebase-admin");
const functions = require("firebase-functions");
const serviceAccount = require("./service_account.json");
const friendProcess = require("./friend_process");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://projec-name.firebaseio.com"
});
exports.friendProcess = functions.firestore.document("users/{userId}/friends/{requestId}").onUpdate(friendProcess);

此代码为 friend_process.js
const admin = require("firebase-admin");
module.exports = function(event) {
const userId = event.params.userId;
const updatedData = event.data.data();
const requestId = updatedData.fromUid;
const previousData = event.data.previous.data();
console.log("update data", updatedData);
console.log("requestId", requestId);
console.log("userId", userId);
if (previousData.status === "request" && updatedData.status === false) {
console.log("updatedData.action ", updatedData);
console.log("ref", event.data.ref);
return admin
.firestore()
.collection("users")
.doc(requestId)
.collection("friends")
.doc(userId)
.get()
.then(doc => {
return console.log("doc ", doc);
})
.catch(error => {
return console.log("error ", error);
});
}
return;
};

这是我的控制台日志
错误:获取应用程序默认凭据时出现意外错误:无法加载默认凭据。浏览到https://developers.google.com/accounts/docs/application-default-credentials以获取更多信息。 在GoogleAuth。 (/user_code/node_modules/firebase-admin/node_modules/google-auth-library/build/src/auth/googleauth.js:229:31) 在步骤(/user_code/node_modules/firebase-admin/node_modules/google-auth-library/build/src/auth/googleauth.js:47:23) at Object.next(/user_code/node_modules/firebase-admin/node_modules/google-auth-library/build/src/auth/googleauth.js:28:53) at fulfilled(/user_code/node_modules/firebase-admin/node_modules/google-auth-library/build/src/auth/googleauth.js:19:58) at process._tickDomainCallback(internal / process / next_tick.js:135:7)
我的package.json
"firebase-admin": "^5.11.0",
"firebase-functions": "^0.9.1",