我有我當前的代碼
let array = {};
array[date] = [
{
Name: Name,
Cell: Cell,
Age: Age,
userId: userId,
Time: time,
Type: type,
read: false,
},
];
像這樣上傳到firebase

但是每次我為同一日期添加一個新條目時,它都會覆寫現有資料。我希望將資料添加到地圖“1”等,而不是覆寫地圖“0”中的資料
更新:我嘗試了以下操作,但收到錯誤訊息。不確定我是否正確地做到了這一點,我仍在學習。
let array = {};
array[date] = [];
try {
await firebase
.firestore()
.collection("Notifications")
.doc(`${helper}`)
.update(
{
array: firebase.firestore.FieldValue.arrayUnion({
Name: Name,
Cell: Cell,
Age: Age,
userId: userId,
Time: time,
Type: type,
read: false,
}),
},
{ merge: true }
);
} catch (e) {
alert(e);
console.log(e);
}
推送更新:
// at beginning of script
let array = {};
// later, when you want to add to it
if (!array[date]) {
array[date] = [];
}
array[date].push({
Name: Name,
Cell: Cell,
Age: Age,
userId: userId,
Time: time,
Type: type,
read: false,
});
try {
await firebase
.firestore()
.collection("Notifications")
.doc(`${helper}`)
.set(
array,
{ merge: true }
);
} catch (e) {
alert(e);
console.log(e);
}
},
解決方案:
try {
await firebase
.firestore()
.collection("Notifications")
.doc(`${helper}`)
.update({
[date]: firebase.firestore.FieldValue.arrayUnion({
Name: Name,
Cell: Cell,
Age: Age,
userId: userId,
Time: time,
Type: type,
read: false,
}),
});
} catch (e) {
alert(e);
console.log(e);
}
uj5u.com熱心網友回復:
由于您設定了整個2022-01-03欄位,因此您將覆寫該欄位中的任何現有值。
如果要將指定的新值/物件與欄位中的現有值合并,請使用array-union運算子。
uj5u.com熱心網友回復:
使用push()添加到一個陣列。
// at beginning of script
let array = {};
// later, when you want to add to it
if (!array[date]) {
array[date] = [];
}
array[date].push({
Name: Name,
Cell: Cell,
Age: Age,
userId: userId,
Time: time,
Type: type,
read: false,
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/385223.html
標籤:javascript 火力基地 反应原生 谷歌云firestore
上一篇:如何修復錯誤:“Null”型別不是“()=>void”型別的子型別
下一篇:單擊FCM通知時轉到不同的活動
