After 7th child Realtime database “convert” to json array

自闭症网瘾萝莉.ら 提交于 2021-01-29 04:30:45

问题


I'm trying to create a job to execute each day, this procedure will create a child DAY and add many children HOUR inside DAY, the problem is when I try to add more than 7 child, Realtime Database converts my JSON tree in an array.

I want this JSON result:

{
  "CALENDAR" : {
    "8" : {
      "AVAILABLE" : true,
      "PURCHASEKEY" : "NA",
      "TIME" : "8:00",
      "TITLE" : "NA",
      "USERID" : "NA",
      "USERNAME" : "NA"
    },
    "9" : {
      "AVAILABLE" : true,
      "PURCHASEKEY" : "NA",
      "TIME" : "9:00",
      "TITLE" : "NA",
      "USERID" : "NA",
      "USERNAME" : "NA"
    }

    .
    .
    .

    "19" : {
      "AVAILABLE" : true,
      "PURCHASEKEY" : "NA",
      "TIME" : "19:00",
      "TITLE" : "NA",
      "USERID" : "NA",
      "USERNAME" : "NA"
    }
  }
}

But if i insert until HOUR 19, result of insert data on Realtime Database is this:

{
  "CALENDAR" : [ null, null, null, null, null, null, null, null, {
    "AVAILABLE" : true,
    "PURCHASEKEY" : "NA",
    "TIME" : "8:00",
    "TITLE" : "NA",
    "USERID" : "NA",
    "USERNAME" : "NA"
  }, {
    "AVAILABLE" : true,
    "PURCHASEKEY" : "NA",
    "TIME" : "9:00",
    "TITLE" : "NA",
    "USERID" : "NA",
    "USERNAME" : "NA"
  }, 

   .
   .
   .

  {
    "AVAILABLE" : true,
    "PURCHASEKEY" : "NA",
    "TIME" : "19:00",
    "TITLE" : "NA",
    "USERID" : "NA",
    "USERNAME" : "NA"
  } ]
}

If i add just 7 child works fine, the result is this:

{
  "CALENDAR" : {
    "8" : {
      "AVAILABLE" : true,
      "PURCHASEKEY" : "NA",
      "TIME" : "8:00",
      "TITLE" : "NA",
      "USERID" : "NA",
      "USERNAME" : "NA"
    },
    "9" : {
      "AVAILABLE" : true,
      "PURCHASEKEY" : "NA",
      "TIME" : "9:00",
      "TITLE" : "NA",
      "USERID" : "NA",
      "USERNAME" : "NA"
    }, 

    .
    .
    .

    "14" : {
      "AVAILABLE" : true,
      "PURCHASEKEY" : "NA",
      "TIME" : "14:00",
      "TITLE" : "NA",
      "USERID" : "NA",
      "USERNAME" : "NA"
    }
  }
}

I'm using Function to create this data, my code is:

exports.createDayHour = functions.pubsub.schedule('every day 00:00').onRun(async context => {
        var day = moment().add(2,'days').format('dddd')
        var _day = moment().add(3,'days').format('DDMMYYYY')

        if(day !== 'Saturday' || day !== 'Sunday'){
            await admin
                .database()
                .ref('CALENDAR')
                .set({
                        8 : {
                            "AVAILABLE" : true,
                            "PURCHASEKEY" : "NA",
                            "TIME" : "8:00",
                            "TITLE" : "NA",
                            "USERID" : "NA",
                            "USERNAME" : "NA"
                        },
                        9 : {
                            "AVAILABLE" : true,
                            "PURCHASEKEY" : "NA",
                            "TIME" : "9:00",
                            "TITLE" : "NA",
                            "USERID" : "NA",
                            "USERNAME" : "NA"
                        },
                        10 : {
                            "AVAILABLE" : true,
                            "PURCHASEKEY" : "NA",
                            "TIME" : "10:00",
                            "TITLE" : "NA",
                            "USERID" : "NA",
                            "USERNAME" : "NA"
                        },
                        11 : {
                            "AVAILABLE" : true,
                            "PURCHASEKEY" : "NA",
                            "TIME" : "11:00",
                            "TITLE" : "NA",
                            "USERID" : "NA",
                            "USERNAME" : "NA"
                        },
                        12 : {
                            "AVAILABLE" : true,
                            "PURCHASEKEY" : "NA",
                            "TIME" : "12:00",
                            "TITLE" : "NA",
                            "USERID" : "NA",
                            "USERNAME" : "NA"
                        },
                        13 : {
                            "AVAILABLE" : true,
                            "PURCHASEKEY" : "NA",
                            "TIME" : "13:00",
                            "TITLE" : "NA",
                            "USERID" : "NA",
                            "USERNAME" : "NA"
                        }
                })
        }
    });

I tried so many ways, using FOR looping, async func..but not success. Please someone know what wrong of my code?


回答1:


What you're seeing is the array-coercion that the Firebase clients do on data that looks like an array. There's no way to enable/disable it, but you can bypass it by ensuring your keys don't look like array indexes.

The simplest way to do that is to prefix all your keys with a fixed string, like "hour_". So:

{
  "CALENDAR" : {
    "hour_08" : {
      "AVAILABLE" : true,
      "PURCHASEKEY" : "NA",
      "TIME" : "8:00",
      "TITLE" : "NA",
      "USERID" : "NA",
      "USERNAME" : "NA"
    },
    "hour_09" : {
      "AVAILABLE" : true,
      "PURCHASEKEY" : "NA",
      "TIME" : "9:00",
      "TITLE" : "NA",
      "USERID" : "NA",
      "USERNAME" : "NA"
    }, 

This makes the keys be real strings, so the Firebase clients won't mistake them for array indices anymore.

As you can see in the example, I'd also recommend padding the values to always be 2 digits, so that they're always the same length. This might not be needed now, but it may be very helpful at some point.




回答2:


I found that you can avoid array-coercion by putting a dummy data with index of string in the same level.

For example,

{
  "CALENDAR" : {
    "8" : {
      "AVAILABLE" : true,
      "PURCHASEKEY" : "NA",
      "TIME" : "8:00",
      "TITLE" : "NA",
      "USERID" : "NA",
      "USERNAME" : "NA"
    },
    "9" : {
      "AVAILABLE" : true,
      "PURCHASEKEY" : "NA",
      "TIME" : "9:00",
      "TITLE" : "NA",
      "USERID" : "NA",
      "USERNAME" : "NA"
    }, 

    .
    .
    .

    "14" : {
      "AVAILABLE" : true,
      "PURCHASEKEY" : "NA",
      "TIME" : "14:00",
      "TITLE" : "NA",
      "USERID" : "NA",
      "USERNAME" : "NA"
    },
// Put a dummy object like this.
    "DUMMY": "NA"
  }
}

by doing this, you don't need to change the data format of other data in the same layer, but if you need to fetch whole CALENDAR object, you need to change your code to discard the dummy data.

I know this is not a best solution, I just want to give an idea to anyone who has same problem.



来源:https://stackoverflow.com/questions/60046070/after-7th-child-realtime-database-convert-to-json-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!