Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

javascript - Axios - Params - Problem with passing the param values in quotes "paramvalue"

I'm stuck with an axios get request to my firebase realtime database Rest endpoint.

The fetching with postman works well using three parameters (see below) and following produced final url :

https://projectname.firebaseio.com/latestMeasurements.json?orderBy="$key"&startAt="key000010"&endAt="key000020"

Subsequently I created following simple axios request. For the "startAt" and "endAt" I receive the error "Constraint key field must be a valid key name" as it seems the constraints are not translated in quotes. For "orderBy" parameter I receive "orderBy must be a valid JSON encoded path". As the "$key" does also not have quotes in the end url.

I tried multiple ways of passing the params but not one worked. As said, with postman everything works fine...

Any help is appreciated!


       axios({
                method: 'GET',
                url:'https://projectname.firebaseio.com/latestMeasurements.json,
                params: { startAt: "key000010", endAt: "key000020", orderBy : "$key" },
            })
                .then((res) => {
                    console.log("SUCCESSFUL FETCH", res.data);
                })
                .catch((e) => {
                    console.log("ERROR",e);
               
                });

example data under /latestMeasurements:

[{
    "key000001": {
        "city": "Stockholm",
        "country": "SE",
        "lastUpdated": 1522854000000,
        "latitued": "N/A",
        "location": "(Folkungagatan tillf?lligt avst?ngd)",
        "longitued": "N/A",
        "measurements": [
            {
                "parameter": "no2",
                "unit": "μg/m3",
                "value": -99
            },
            {
                "parameter": "pm10",
                "unit": "μg/m3",
                "value": -99
            }
        ]
    },
    "key000002": {
        "city": "Ulaanbaatar",
        "country": "MN",
        "lastUpdated": 1552513500000,
        "latitude": 47.91798,
        "location": "1-r khoroolol",
        "longitude": 106.84806,
        "measurements": [
            {
                "parameter": "co",
                "unit": "μg/m3",
                "value": 57
            },
            {
                "parameter": "no2",
                "unit": "μg/m3",
                "value": 30
            },
            {
                "parameter": "pm10",
                "unit": "μg/m3",
                "value": 199
            },
            {
                "parameter": "pm25",
                "unit": "μg/m3",
                "value": 217
            },
            {
                "parameter": "so2",
                "unit": "μg/m3",
                "value": 21
            }
        ]
    }
}
]

error fetching frontend (here orderBy is fixed in url and not passed as a param to only produce the error on the other parameters) :

enter image description here

reproduction of this error in postman (param values not in quotes):

enter image description here

success in postman (param values in quotes):

enter image description here

question from:https://stackoverflow.com/questions/65643638/axios-params-problem-with-passing-the-param-values-in-quotes-paramvalue

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I can't reproduce the problem, as I get the expected results here: https://stackoverflow.firebaseio.com/65643638.json?orderBy=%22$key%22&startAt=%221%22&endAt=%2210%22&print=pretty

I think you're struggling with the fact that Firebase Realtime Database keys are always strings, while you are trying to treat them as a sequential numeric (array) indices. Firebase experts strongly recommend against using array indexes like this, for example in this classic blog post: Best Practices: Arrays in Firebase.

If you must use such indexes, you'll need to ensure that you store them in a format where the lexicographical sort order is the same as the numerical sort order. I typically recommend:

  1. Prefixing each numeric key with a short alphanumeric prefix, to prevent Firebase from treating the keys as an array.
  2. Pad the number values to a fixed length, so that the lexicographical order and numerical order of the keys become the same.

When combined, your JSON becomes:

{ 
  "key001": {
    "city" : "天津市",
    ...
  },
  "key002": {
    "city" : "Stockholm",
    ...
  }
}

And then your query becomes:

params: { startAt: 'key001', endAt: 'key010', orderBy : "$key" }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...