如何在 firebase firestore 中获取内部集合
How to get inner collection in firebase firestore
我正在尝试获取 firestore 中特定用户的设备令牌,该设备令牌存储在“客户”或“律师”集合中的令牌集合中。
当我从链中删除第二个 .collection("tokens") 时,我得到了用户对象,但是随着链中的令牌集合,我似乎无法获得任何用户(客户或律师)回来,即使用户和它的令牌存在。我做错了什么
exports.onReceiveChatMessage = functions.database
.ref("/messages/{uid}")
.onCreate(async (snapshot, context) => {
const newMessage = snapshot.val();
console.log("NEW_MESSAGE", newMessage);
const senderName = newMessage.sender_name;
const messageContent = newMessage.content;
console.log("SENDER'S_NAME", senderName);
console.log("MESSAGE_BODY", messageContent);
const uid = context.params.uid;
console.log("RECEIVERS_ID", uid);
if (newMessage.sender_id == uid) {
//if sender is receiver, don't send notification
console.log("sender is receiver, dont send notification...");
return;
} else if (newMessage.type === "text") {
console.log(
"LETS LOOK FOR THIS USER, STARTING WITH CLIENTS COLLECTION..."
);
let userDeviceToken;
await firestore
.collection("clients")
.doc(uid)
.collection("tokens")
.get()
.then(async (snapshot) => {
if (!snapshot.exists) {
console.log(
"USER NOT FOUND IN CLIENTS COLLECTION, LETS CHECK LAWYERS..."
);
await firestore
.collection("lawyers")
.doc(uid)
.collection("tokens")
.get()
.then((snapshot) => {
if (!snapshot.exists) {
console.log(
"SORRY!!!, USER NOT FOUND IN LAWYERS COLLECTION EITHER"
);
return;
} else {
snapshot.forEach((doc) => {
console.log("LAWYER_USER_TOKEN=>", doc.data());
userDeviceToken = doc.data().token;
});
}
});
} else {
snapshot.forEach((doc) => {
console.log("CLIENT_USER_TOKEN=>", doc.data());
userDeviceToken = doc.data().token;
});
}
});
// console.log("CLIENT_DEVICE_TOKEN", userDeviceToken);
} else if (newMessage.type === "video_session") {
}
})
这一行
if (!snapshot.exists) {
应该是:
if (snapshot.empty) {
因为您是在 CollectionReference
(returns 和 QuerySnapshot
)上调用 get()
,而不是在 DocumentReference
(returns一个DocumentSnapshot
).
如果在示例中从链中删除 .collection('tokens')
,它确实有效,因为 DocumentSnapshot
确实有成员 exists
,但 CollectionReference
没有t.
在这里看看他们的成员:
https://googleapis.dev/nodejs/firestore/latest/CollectionReference.html#get
然后:
https://googleapis.dev/nodejs/firestore/latest/QuerySnapshot.html
作为建议,我曾经混淆快照并因为使用 Javascript 而不是 Typescript 而遇到这个问题。所以我习惯了调用文档时调用结果 snap
,调用集合时调用结果 snaps
。这让我想起了我正在研究什么样的反应。像这样:
// single document, returns a DocumentSnapshot
const snap = await db.collection('xyz').doc('123').get();
if (snap.exists) {
snap.data()...
}
// multiple documents, returns a QuerySnapshot
const snaps = await db.collection('xyz').get();
if (!snaps.empty) { // 'if' actually not needed if iterating over docs
snaps.forEach(...);
// or, if you need to await, you can't use the .forEach loop, use a plain for:
for (const snap of snaps.docs) {
await whatever(snap);
}
}
我正在尝试获取 firestore 中特定用户的设备令牌,该设备令牌存储在“客户”或“律师”集合中的令牌集合中。 当我从链中删除第二个 .collection("tokens") 时,我得到了用户对象,但是随着链中的令牌集合,我似乎无法获得任何用户(客户或律师)回来,即使用户和它的令牌存在。我做错了什么
exports.onReceiveChatMessage = functions.database
.ref("/messages/{uid}")
.onCreate(async (snapshot, context) => {
const newMessage = snapshot.val();
console.log("NEW_MESSAGE", newMessage);
const senderName = newMessage.sender_name;
const messageContent = newMessage.content;
console.log("SENDER'S_NAME", senderName);
console.log("MESSAGE_BODY", messageContent);
const uid = context.params.uid;
console.log("RECEIVERS_ID", uid);
if (newMessage.sender_id == uid) {
//if sender is receiver, don't send notification
console.log("sender is receiver, dont send notification...");
return;
} else if (newMessage.type === "text") {
console.log(
"LETS LOOK FOR THIS USER, STARTING WITH CLIENTS COLLECTION..."
);
let userDeviceToken;
await firestore
.collection("clients")
.doc(uid)
.collection("tokens")
.get()
.then(async (snapshot) => {
if (!snapshot.exists) {
console.log(
"USER NOT FOUND IN CLIENTS COLLECTION, LETS CHECK LAWYERS..."
);
await firestore
.collection("lawyers")
.doc(uid)
.collection("tokens")
.get()
.then((snapshot) => {
if (!snapshot.exists) {
console.log(
"SORRY!!!, USER NOT FOUND IN LAWYERS COLLECTION EITHER"
);
return;
} else {
snapshot.forEach((doc) => {
console.log("LAWYER_USER_TOKEN=>", doc.data());
userDeviceToken = doc.data().token;
});
}
});
} else {
snapshot.forEach((doc) => {
console.log("CLIENT_USER_TOKEN=>", doc.data());
userDeviceToken = doc.data().token;
});
}
});
// console.log("CLIENT_DEVICE_TOKEN", userDeviceToken);
} else if (newMessage.type === "video_session") {
}
})
这一行
if (!snapshot.exists) {
应该是:
if (snapshot.empty) {
因为您是在 CollectionReference
(returns 和 QuerySnapshot
)上调用 get()
,而不是在 DocumentReference
(returns一个DocumentSnapshot
).
如果在示例中从链中删除 .collection('tokens')
,它确实有效,因为 DocumentSnapshot
确实有成员 exists
,但 CollectionReference
没有t.
在这里看看他们的成员:
https://googleapis.dev/nodejs/firestore/latest/CollectionReference.html#get
然后:
https://googleapis.dev/nodejs/firestore/latest/QuerySnapshot.html
作为建议,我曾经混淆快照并因为使用 Javascript 而不是 Typescript 而遇到这个问题。所以我习惯了调用文档时调用结果 snap
,调用集合时调用结果 snaps
。这让我想起了我正在研究什么样的反应。像这样:
// single document, returns a DocumentSnapshot
const snap = await db.collection('xyz').doc('123').get();
if (snap.exists) {
snap.data()...
}
// multiple documents, returns a QuerySnapshot
const snaps = await db.collection('xyz').get();
if (!snaps.empty) { // 'if' actually not needed if iterating over docs
snaps.forEach(...);
// or, if you need to await, you can't use the .forEach loop, use a plain for:
for (const snap of snaps.docs) {
await whatever(snap);
}
}