gpt4 book ai didi

javascript - 使用 Javascript 获取存储在 Firebase 中的对象的子对象

转载 作者:行者123 更新时间:2023-12-03 00:33:41 26 4
gpt4 key购买 nike

我有一组简单的 Java 对象(来自 Android 设备的设备 token )作为文档存储在 Firebase 中:

public class DeviceToken {
String tokenID;


public DeviceToken() {
}

public DeviceToken(String tokenID) {
this.tokenID = tokenID;
}


public String getTokenID() {
return tokenID;
}

public void setTokenID(String tokenID) {
this.tokenID = tokenID;
}
}

enter image description here

我正在尝试将此对象的 tokenID 子项作为 JavaScript 中的字符串获取以下云函数:

exports.sendDMNotification = functions.firestore.document('/dm_threads/{thread_id}/messages/{message_id}')
.onCreate((snapshot, context) => {


const newMessage = snapshot.data();

const senderName = newMessage.authorName;
const senderID = newMessage.authorUID;
const messageText = newMessage.message;
const recipientName = newMessage.recipientName;
const recipientID = newMessage.recipientUID;
const timestamp = newMessage.timestamp;


let deviceTokenQuery = admin.firestore().collection(`/users/${recipientID}/device_tokens/`);

return deviceTokenQuery.get().then(querySnapshot => {

let tokenShapshot = querySnapshot.docs;

const notificationPromises = tokenShapshot.map(token => {


let token_id = token['tokenID'];

console.log(token_id);
console.log(token)
console.log(JSON.stringify(token));


const payload = {
notification: {
title: senderName,
body: messageText,
icon: "default"
}
};


return admin.messaging().sendToDevice(token_id, payload)

});

return Promise.all(notificationPromises);

});

});

第一个日志语句返回未定义,第二个返回 [object Object],第三个返回似乎是文档引用的一堆元数据,但没有我正在使用的对象的任何属性。如何检索我正在获取的对象文档的 tokenID 子项?

最佳答案

由于您在 querySnapshot.docs 上调用 map,我假设传递给 map 回调的每个项目都是一个文档。在这种情况下,您仍然需要调用 data() 来获取文档的数据:

let tokenShapshot = querySnapshot.docs;

const notificationPromises = tokenShapshot.map(doc => {

let token_id = doc.data().tokenID;

关于javascript - 使用 Javascript 获取存储在 Firebase 中的对象的子对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53747205/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com