[英]How can I reach and manipulate Firebase Database from Cloud Functions?
I am working on sending dynamic notification to user.我正在努力向用户发送动态通知。 Therefore, I am dealing with the Firebase Cloud Functions.
因此,我正在处理 Firebase Cloud Functions。 Here is my question: How can I reach database in the cloud functions side?
这是我的问题:如何在云功能端访问数据库?
Here is my codes:这是我的代码:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
// exports.helloWorld = functions.https.onRequest((request, response) => {
// response.send("Hello from Firebase!");
// });
admin.initializeApp(functions.config().functions);
var newData;
//exports.myTrigger= functions.firestore.document().
exports.notificationTrigger = functions.firestore.document(`notifications/{notificationId}`).onCreate(async (snapshot, context) => {
if (snapshot.empty) {
console.log('No Devices');
return;
}
var notificationData = snapshot.data();
// notificationData.email;
console.log(`Data is : ${notificationData.email}`);
admin.database().ref(`users/${notificationData.email}/deviceToken`).then((snapshot) => {
var token = snapshot.data();
console.log('token',token)
return 1;
}).catch(err => {
console.error(err);
});
var tokens = [
`${token.deviceToken}`
];
var payload = {
notification: {
title: `Yeni Eklenen Değerin Maili : ${notificationData.email}`,
body: 'hüsen',
sound: 'default',
},
data: {
click_action: 'FLUTTER_NOTIFICATION_CLICK',
Messages:'Baba Mesajı from otomatik fonksiyon'
},
};
try {
const response = await admin.messaging().sendToDevice(tokens, payload);
console.log('Notification sent successfully');
} catch (err) {
console.log(err);
}
});
As you can see, Firstly, I want to take email from nofications collection, then using that email, I want to search user which is using that email, After that, I want to take user deviceToken.如您所见,首先,我想从 nofications 集合中获取 email,然后使用该 email,我想搜索正在使用该 email 的用户,之后,我想获取用户 deviceToken。
I tried so much things about syntax but I did not get any result from them:/我尝试了很多关于语法的东西,但我没有从中得到任何结果:/
Likewise, I took some kind of errors:同样,我犯了一些错误:
Here you can see the errors:在这里你可以看到错误:
The error message is telling you what went wrong:错误消息告诉您出了什么问题:
Reference.child failed: First argument was an invalid path = "users/patientaccount@gmail.com/deviceToken".
Reference.child 失败:第一个参数是无效路径 =“users/patientaccount@gmail.com/deviceToken”。 Paths must be non-empty strings and can't contain ".", "#", "$", "[", or "]"
路径必须是非空字符串,不能包含“.”、“#”、“$”、“[”或“]”
The path that includes the email address has an invalid character .
包含 email 地址的路径包含无效字符
.
. .
If you want to store per-user data, you should instead use the user account's unique ID.如果您想存储每个用户的数据,您应该使用用户帐户的唯一 ID。 Email addresses do not make very good unique identifier for a variety of reasons.
由于各种原因,Email 地址并不是很好的唯一标识符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.