[英]How to push an object in an array with arrayUnion() in Firebase Firestore?
[英]How to append an array of strings to an array in Firebase using arrayUnion (TypeScript)
我正在尝试在 TypeScript 中编写的 Firebase Cloud Function 中使用 arrayUnion。每当我添加数组时,它也会添加变量“items”的名称。 不知道为什么会这样或我做错了什么。
附加 2 个字符串后我的数据库的图片(这显然是错误的):
我的目标是将字符串列表添加到我的数据库中。 联合数组中不应有任何重复的 id。
Firbase Function 被成功调用 Array items = ['00000000000009', '00000000000008']
//Defined in class constructor
private firestore: FirebaseFirestore.Firestore
async addItemsToWishlist(uid: string, items: string[]): Promise<boolean> {
try {
// Nothing Added Function called for no reason
if (items.length == 0) {
return true;
}
console.log("Called Successfull with items: ", items);
console.log("items length: ", items.length);
// Add the items to the corresponding users Wishlist
await this.firestore.collection("users").doc(uid).update({
wishlistedItems: admin.firestore.FieldValue.arrayUnion(items),
});
// Return true if successful
return true;
} catch (e) {
// Log the Error on the Console
console.error(e);
return false;
}
}
也不知道为什么我不能得到我的数组的长度。 控制台日志:
从日志 output 看来,您的items
是一个包含items
子数组的object ,因此您需要在将其传递给 Firestore 之前将其解包:
await this.firestore.collection("users").doc(uid).update({ // 👇
wishlistedItems: admin.firestore.FieldValue.arrayUnion(items.items),
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.