Firebase数据库更新正在删除子数据

时间:2017-08-04 17:28:17

标签: javascript angular firebase firebase-realtime-database ionic3

我正在编写一个事件列表应用程序,它具有Event>的层次结构。部分>步。在我创建了一个活动之后,我可以进入"该活动并查看"部分列表"其中包含"步骤"。

的列表

我构建数据库的方式是,我有一个按用户ID排序的对象来显示事件及其详细信息,然后如果我点击该事件,我会进入它,现在看一个{ {1}}对象。 parts对象看起来像这样:

parts

这很有效,我按事件ID提取所有部分。问题来自于步骤。当我向零件添加一个步骤时,我把它放在两个地方(使用parts: eventID1: partID1: startDateTime:value, endDateTime:value, title:value, ... partID2: startDateTime:value, endDateTime:value, title:value, ... etc. );一个在步骤表中,一个在部分本身,添加一个新的键:值update

steps对象如下所示:

part

最后(问题区域)部件对象更新为如下所示:

steps:
    eventID1:
        partID1:
            stepID1:
                title:value,
                startDateTime:value,
                etc.
            stepID2:
                title:value,
                startDateTime:value,
                etc.

我已经添加了上述部分中的步骤(再次使用parts: eventID1: partID1: startDateTime:value, endDateTime:value, title:value, ... steps: stepID1: title:value, startDateTime:value, etc. stepID2: title:value, startDateTime:value, etc. partID2: startDateTime:value, endDateTime:value, title:value, ... etc. )。问题是,当我编辑部件的标题或日期时,它会返回更新,但内部没有任何步骤。它会删除这些步骤。

每个updateeventpart具有相同的结构,因此我将所有这些组合成一个函数(使用离子3 / angular4)

step

这基本上是迭代,特定的 update(action, type, eid, pid, sid, title, location, startDateTime) { console.log("Firebase Database Receiving: ", action, type, eid, pid, sid, title, location, startDateTime); let key = (action === "add") ? this.ref.push().key : null; console.log(key); let update; let object = { startDateTime: startDateTime, startDateTimeEpoch: moment(startDateTime).format("X"), endDateTime: startDateTime, endDateTimeEpoch: moment(startDateTime).format("X"), title: title, type: type, roles: { [this.firebaseAuthentication.uid]: "admin", }, location: { title: location, street: '', city: '', state: '', zip: '', timeZone: '' }, }; if(type === "events"){ key = (key) ? key : eid; if(action !== "delete") { update = { ['events/' + key]: object, ['roles/' + this.firebaseAuthentication.uid + '/' + key]: object } } else { console.log("delete event", key); update = { ['events/' + key]: null, ['roles/' + this.firebaseAuthentication.uid + '/' + key]: null, ['parts/' + key]: null, ['steps/' + key]:null } } } else if(type === "parts") { key = (key) ? key : pid; if(action !== "delete") { update = { /************ . Here is the update object . ***********/ ['parts/' + eid + '/' + key]: object } } else { update = { ['parts/' + eid + '/' + key]: null, ['steps/' + eid + '/' + key]: null } } } else if(type === "steps") { key = (key) ? key : sid; if(action !== "delete") { update = { ['parts/' + eid + '/' + pid + '/steps/' + key]: object, ['steps/' + eid + '/' + pid + '/' + key]: object } } else { update = { ['parts/' + eid + '/' + pid + '/steps/' + key]: null, ['steps/' + eid + '/' + pid + '/' + key]: null } } } console.log(object, update); return new Promise((resolve, reject) => { /**************** . Finally the actual update function ************/ this.ref.update(update).then(success => { resolve(success); }, error => { reject(error); }); }); } 组合是action/type / edit

根据我对firebase的part的了解,它不应该破坏对象中没有触及的任何数据。这就像update()那样,但我显然不希望这样。

有什么想法?弗兰克·

2 个答案:

答案 0 :(得分:1)

从我从代码中收集到的内容,更新功能正如我所期望的那样(来自经验而不是文档)。我的意思是通过将其设置为对象来更新密钥就是这样,它将键值设置为您传递的对象。一个例子:

用户:

  • NIK

    • 年龄:18
    • ID:1
  • 乔治

    • 年龄:29
    • ID:2

让obj = {age:24};

调用update({nik:obj})实际上会将nik设置为具有您提供的对象的值(从而删除id)。有理由对吗?好firebase确实说更新:

  

如果要同时写入数据库位置的多个子节点而不覆盖其他子节点,可以使用update方法,如下所示:

可能不清楚的是,它意味着与您正在更新的密钥处于同一级别的其他子节点(这将是乔治)。

答案 1 :(得分:0)

根据到目前为止我对firebase的使用情况,这是导致此错误的“空值”,这样我就可以在单个更新调用中更新对象并从对象中删除密钥对。

object: {

 key1: value1, 

key2: null // this key will be removed as it is set to null

}

希望这对某人有帮助