使用Javascript获取Firebase中存储的对象的子对象
我有一组简单的Java对象(Android设备的设备令牌)作为文档存储在Firebase中:使用Javascript获取Firebase中存储的对象的子对象,javascript,node.js,google-cloud-firestore,google-cloud-functions,Javascript,Node.js,Google Cloud Firestore,Google Cloud Functions,我有一组简单的Java对象(Android设备的设备令牌)作为文档存储在Firebase中: public class DeviceToken { String tokenID; public DeviceToken() { } public DeviceToken(String tokenID) { this.tokenID = tokenID; } public String getTokenID() {
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;
}
}
我
我正在尝试将此对象的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);
});
});
第一个log语句返回undefined,第二个返回[object object],第三个返回文档引用中的一组元数据,但在我使用对象时没有任何属性。如何检索我正在获取的对象文档的
tokenID
子项?既然您在querySnapshot.docs
上调用map
,我假设传递给map
回调的每个项都是一个文档。在这种情况下,您仍然需要调用data()
来获取文档的to数据:
let tokenShapshot = querySnapshot.docs;
const notificationPromises = tokenShapshot.map(doc => {
let token_id = doc.data().tokenID;
如果我没有弄错的话,是不是
tokenShapshot.map(token=>({…})
ecapsulation()?还有..`但是我正在使用的对象没有任何属性`你能告诉我们属性是什么吗?我需要的就是这个方法。再次感谢你。