我想发送触发 pengumuman 主题的通知。
export const onNotifPengumuman = functions.database.ref('/pengumuman_course/{course_id_p}/{pengumuman_id}')
.onCreate((snapshot,context) =>{
const course_id_p = context.params.course_id_p;
const pengumuman_id = context.params.pengumuman_id;
const nama_matkul = admin.database().ref('/courses/'+course_id_p+'name').once('value').then(snap =>{
return snapshot.val();
}).catch(error =>
{
console.log(error);
})
console.log(`cobacobacoba ${nama_matkul}`);
return admin.database().ref('pengumuman/' + pengumuman_id + '/').once('value').then(snap =>{
const pengumumanData = snap.val();
const notifDataPengumuman = {
data:{
data_type: "pengumuman ",
title: "Pengumuman Baru", // data bebas (key, value)
body: `${nama_matkul}`, // chatId = const chatId
sound: "default"
}
}
return admin.messaging().sendToTopic(course_id_p, notifDataPengumuman)
.then(function(response) {
console.log("Successfully sent message:", response);
})
.catch(function(error) {
console.log("Error sending message:", error);
});
}).catch(error => {
console.log(error);
})
});
在第一个引用 functions.database.ref('/pengumuman_course/{course_id_p}/{pengumuman_id}')
我想在 firebase 实时数据库中访问并触发这个子项,代码如下:
在此代码之后 return admin.database().ref('pengumuman/' + pengumuman_id + '/')
我试图获取有关 pengumuman 的所有信息并将其发送到用户。代码如下:
enter image description here
但在此之前,我想在数据库中的类(class)引用中获取pengumuman
名称,以获取名称值,使用以下代码:
const nama_matkul = admin.database().ref('/courses/'+course_id_p+'name').once('value').then(snap =>{
return snapshot.val();
}).catch(error =>
{
console.log(error);
})
问题是当我使用该代码获取子名称并将其存储到 matkul 中时,当我发送/记录时,它将返回 Promise 对象。我想要显示“REKAYASA PERANGKAT LUNAK”的结果。 感谢并抱歉错误的解释
[已修复]
我正在尝试解决方案并找到了这段代码
export const onNotifPengumuman = functions.database.ref('/pengumuman_course/{course_id_p}/{pengumuman_id}')
.onCreate((snapshot,context) =>{
const course_id_p = context.params.course_id_p;
console.log(`course id pengumuman ${course_id_p}`);
const pengumuman_id = context.params.pengumuman_id;
admin.database().ref('/courses/' + course_id_p + '/').once('value').then(snap2 =>{
const nama_matkul = snap2.child('name').val();
console.log(`nama matkul dari sini ${nama_matkul}`);
admin.database().ref('pengumuman/' + pengumuman_id + '/').once('value').then(snap =>{
const pengumumanData = snap.val();
const notifDataPengumuman = {
data:{
data_type: "pengumuman",
title: "Pengumuman Baru", // data bebas (key, value)
body: `Judul :${nama_matkul}`, // chatId = const chatId
sound: "default"
}
}
return admin.messaging().sendToTopic(course_id_p, notifDataPengumuman)
.then(function(response) {
console.log("Successfully sent message:", response);
})
.catch(function(error) {
console.log("Error sending message:", error);
});
}).catch(error => {
console.log(error);
})
}).catch(error =>{
console.log(error);
})
});
最佳答案
您需要解决 Promise 才能获得它们返回的值。您目前正在做的是分配 nama_matkul
promise ,但您永远不会等待它完成。
异步/等待
您可以通过将函数定义为异步来使用 async
/await
:
.onCreate(async (snapshot,context) =>{
// Your asynchronous code here
}
然后您可以通过运行来解决 promise const nama_matkul = (await admin.database().ref('/courses/'+course_id_p+'name').once('value')).val();
如果需要处理异常,请将promise 和await 包装在try catch block 中。
重构代码后,它可能看起来像这样:
export const onNotifPengumuman = functions.database.ref('/pengumuman_course/{course_id_p}/{pengumuman_id}')
.onCreate(async (snapshot,context) => {
try {
const course_id_p = context.params.course_id_p;
const pengumuman_id = context.params.pengumuman_id;
const nama_matkul = (await admin.database().ref('/courses/'+course_id_p+'name').once('value')).val();
console.log(`cobacobacoba ${nama_matkul}`);
const pengumumanData = (await admin.database().ref('pengumuman/' + pengumuman_id + '/').once('value')).val();
const notifDataPengumuman = {
data: {
data_type: "pengumuman ",
title: "Pengumuman Baru", // data bebas (key, value)
body: `${nama_matkul}`, // chatId = const chatId
sound: "default"
}
}
try {
await admin.messaging().sendToTopic(course_id_p, notifDataPengumuman);
console.log("Successfully sent message:", response);
} catch (messageSendError) {
console.log("Error sending message:", messageSendError);
}
} catch (error) {
console.log(error);
}
});
然后/捕获
如果您确实想坚持使用当前的设置并使用回调,则可以保留 .then
调用并在回调中处理应用程序逻辑;您的代码可能如下所示:
export const onNotifPengumuman = functions.database.ref('/pengumuman_course/{course_id_p}/{pengumuman_id}')
.onCreate((snapshot,context) => {
const course_id_p = context.params.course_id_p;
const pengumuman_id = context.params.pengumuman_id;
admin.database().ref('/courses/'+course_id_p+'name').once('value')
.then(nameSnapshot => {
const nama_matkul = nameSnapshot.val();
console.log(`cobacobacoba ${nama_matkul}`);
admin.database().ref('pengumuman/' + pengumuman_id + '/').once('value')
.then(dataSnapshot => {
const pengumumanData = dataSnapshot.val();
const notifDataPengumuman = {
data: {
data_type: "pengumuman ",
title: "Pengumuman Baru", // data bebas (key, value)
body: `${nama_matkul}`, // chatId = const chatId
sound: "default"
}
}
return admin.messaging().sendToTopic(course_id_p, notifDataPengumuman)
.then(response => console.log("Successfully sent message:", response))
.catch(error => console.log("Error sending message:", error));
})
.catch(error => console.log(error));
})
.catch(error => console.log(error))
});
如果您愿意,您当然可以使用 then/catch 和 wait 的组合,这是否是一个好的做法,或者何时使用这实际上取决于您使用它的情况。
关于node.js - 如何只获取子firebase云函数的一个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52382601/