Код: Выделить всё
const admin = require("firebase-admin");
const functions = require("firebase-functions");
admin.initializeApp();
exports.sendNotificationOnNewMessage = functions.firestore
.document("messages/{s}/messages/{r}/pm/{id}")
.onCreate(async (snap, context) => {
const messageData = snap.data();
const {s, r} = context.params;
const u = await admin.firestore().collection("users").doc(r).get();
if (!u.exists) {
console.log(`User ${r} not found in users collection.`);
return null;
}
const receiverToken = u.data().token;
if (!receiverToken) {
console.log(`Skipping notification.`);
return null;
}
const payload = {
notification: {
title: `New message from ${s}`,
body: messageData.content,
},
token: u.data().token,
};
try {
await admin.messaging().send(payload);
console.log("Notification sent successfully");
} catch (error) {
console.error("Error sending notification:", error);
}
return null;
});
< /code>
Также у меня есть правила Firestore, как ниже < /p>
match /messages/{s}/messages/{r}/pm/{id} {
allow read, write: if request.auth != null &&
(request.auth.uid == s || request.auth.uid == r);
}
< /code>
Я продолжаю получать эту ошибку < /p>
TypeError: functions.firestore.document is not a function
at Object. (/Users/x/Desktop/y/functions/index.js:5:6)
at Module._compile (node:internal/modules/cjs/loader:1562:14)
at Object..js (node:internal/modules/cjs/loader:1699:10)
at Module.load (node:internal/modules/cjs/loader:1313:32)
at Function._load (node:internal/modules/cjs/loader:1123:12)
at TracingChannel.traceSync (node:diagnostics_channel:322:14)
at wrapModuleLoad (node:internal/modules/cjs/loader:217:24)
at Module.require (node:internal/modules/cjs/loader:1335:12)
at require (node:internal/modules/helpers:136:16)
Error: Functions codebase could not be analyzed successfully. It may have a syntax or runtime error
Подробнее здесь: https://stackoverflow.com/questions/794 ... a-function