Firebase REST API working with shallow data

2020-05-04 10:17发布

问题:

I am looking at the documentation for the Firebase REST API and it states that

Shallow - This is an advanced feature, designed to help you work with large datasets without needing to download everything. Set this to true to limit the depth of the data returned at a location. If the data at the location is a JSON primitive (string, number or boolean), its value will simply be returned. If the data snapshot at the location is a JSON object, the values for each key will be truncated to true.

However from what I have found it seems to always set the value to true regardless of whether it is a JSON primitive or object?

eg from what I have read I would expect the below call

https://samplechat.firebaseio-demo.com/message_list/-K6ojd3dJQ3AVi36cruT/.json?print=pretty&shallow=true to return

{
    "text" : "Ahoy!",
    "user_id" : "jack"
}

instead it is returning

{
    "text" : true,
    "user_id" : true
}

I understand in this example I provided shallow isn't required as it is the lowest level, however I have found this issue also with my own data at higher levels were everything is returning true.

Is there a way to return the value of a JSON primitive as suggested using the shallow param?

回答1:

When you query the Firebase REST API with shallow=true, it will return true for any key that exists on the level that you query, regardless of the type of the data underneath it. There is no way to change that behavior.

The documentation about primitive values applies when you query one level deeper. In your case, if you run this query: https://samplechat.firebaseio-demo.com/message_list/-K6ojd3dJQ3AVi36cruT/text.json?print=pretty&shallow=true, you get back "Ahoy!"

So:

  • https://samplechat.firebaseio-demo.com/message_list/-K6ojd3dJQ3AVi36cruT/.json?print=pretty

    {
      "text" : "Ahoy!",
      "user_id" : "jack"
    }
    
  • https://samplechat.firebaseio-demo.com/message_list/-K6ojd3dJQ3AVi36cruT/.json?print=pretty&shallow=true

    {
      "text" : true,
      "user_id" : true
    }
    
  • https://samplechat.firebaseio-demo.com/message_list/-K6ojd3dJQ3AVi36cruT/text.json?print=pretty&shallow=true

    "Ahoy!"