簡體   English   中英

如何使用 Firestore 和 Firebase 函數更新“對象數組”?

[英]How to update an "array of objects" with Firestore and Firebase functions?

我有一些身體數據進入 firebase 作為數組['123','hello','34']

然后我通過執行const data = JSON.stringify(['123','hello','34'])處理它

在我通過設置更新 firestore 數據之后

set({testData: firestore.FieldValue.arrayUnion(data)})

問題是,整個東西在 firestore 中保存為字符串

testData: "['123','hello','34']"

而不是數組。

testData: 
 0: "123"
 1: "hello"
 2: "34"

我的問題是如何讓 firestore 將其保存為數組?

您正在使用stringify()將其轉換為字符串。 嘗試重構代碼,如下所示:

docRef.set({ testData : firestore.FieldValue.arrayUnion(...data) })
// use spread operator here                             ^^^

文檔(NodeJS) 有一個使用arrayUnion添加多個值的示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM