【发布时间】:2018-09-20 15:15:49
【问题描述】:
我的 firebase 上保存了一个数组,如下所示:
matches:[ {match:{id:1,data:...}}]
我只想在这个数组的 firebase 上保存一项。例如,我的比赛有 id:32。我想在 firebase 中保存的数组上找到它,然后更改它。
我正在努力做到这一点。但我认为向火力库发出请求、复制数组并再次保存整个数组是非常难看的。
const ref = `/users/${currentUser.uid}/matches`;
var list = [];
firebase.database().ref(ref).on('value', function (snap) { list = snap.val(); });
if (list.length > 0) {
const indexToUpdate = list.findIndex(k => k.name == match.name)
if (indexToUpdate >= 0) {
list[indexToUpdate] = match;
return dispatch => {
firebase
.database()
.ref(`/users/${currentUser.uid}/matches`)
.set(list)
.then(() => {
dispatch({ type: MATCH_UPDATE, payload: match });
});
};
}
}
有灯吗?
【问题讨论】:
-
您尝试了吗
/users/${currentUser.uid}/matches/${indexToUpdate}/Firebase 将数组存储为对象,并在返回客户端时将其转换回数组跨度>
标签: javascript reactjs firebase firebase-realtime-database