The result was not an array. Firebase Real-time Database

Hello fellow low-coders,

I am looking for a piece of advice. We are trying to use the Firebase realtime database for storing messages. The ideal way is to use the HTTP Request to POST a new message in and once we need to display them, use GET to retrieve an array of messages. However, we were unable to make it work, since the response is always -1. We tried @JOHN_WORSHAM advice to set up the key response path, but it doesn’t seem to change anything.

I include the database structure as well as the response here.

POST:

DATABASE STRUCTURE:

RESPONSE KEY PATH:

RESPONSE FROM DATABASE:

As you can see, we are trying to store 3 items per message (all of them are text). Message, route_ref and sender_uid. And then request the entire collection as an array. However, we keep getting this “error” that the result was not an array.

This is the only thing we need to solve before launching our app. If anyone could help or share their solution, we would be extremely thankful.

Thank you in advance.

Your relative path should be in the form of projects/YOURPROJECT/databases/(default)/documents/messages
Then response key path should be documents.

Have you tried using the firestore connector instead of HTTP requests?

Thank you for your reply, however, we are not using Firestore Database, but Firebase Real-time Database, so therefore this solution, unfortunately, doesn’t apply to our issue. Do you have any idea about how to solve that issue within the real-time database?

Thank you in advance.
Jerry

Looking at your database structure, I would either guess your response path would need to be .json.messages. If that doesn’t work, can you show the structure you get from the request before going into the .json response path? Either from a Composer screenshot or postman or whatever you find easiest.

If there is no viable array within your json, then the get collection method can’t be used, I’m afraid. You’ll either need to use the get one, or a direct HTTP node, as you’re attempting in this other thread.

I tried to set the responce key path to .json.message - hope this is what you meant:

The response:

Oh! Take the .json away from the Relative path. Try to change the Response key path to json.messages and json and messages and see if one of these would work better.

@Mevi Hi Mevi, thanks, however non of these works and it makes no difference what we type in the Response Key Path. I think the problem is that Response Key Path filter results ONLY if they are in an object, however we do have an array “messages” and there’s a list of custom ID’s created by Firebase RT DB, therefore that doesn’t work for us.

Can you think of anything else, that could help us out, please? :slight_smile:

This is a bit tricky to work with. The guiding detail here is that the response is an object, and it does not contain any arrays. There are multiple messages stored under different properties/keys, but the underlying data structure is still an object (dictionary / hash table).

You’ll need to use Get record (expects an object) instead of Get collection (expects an array) to match the response type. This gets you the data, but dynamic property names in schemas are not supported. So you cannot have a schema that matches the response structure exactly, and you’ll have to do some manual mapping.

So what you could do is configure the schema for Get collection manually, so that it’s a list of message objects like so:

[{
  id: string, // we'll map the key name into this
  message: string,
  route_ref: string,
  sender_uid: string
}]

The schema for Get record could be automatically set from the response to get at least close and to avoid a bunch of validation errors. You could then map the response into a list of objects with a formula, something like:

MAP<key>(KEYS(messages_object), SET_KEY(messages_object[key], 'id', key))

/*
turns this { 'key1': { ... }, 'key2': { ...}
into this [{ id: 'key1', ... }, { id: 'key2', ... }]
*/

It’s a bit of a pain to work with and takes up your slot/schema for actually getting a single record, so you may want to set up a duplicate resource for this (e.g. MessageList). Unfortunately I don’t think there’s a much simpler way with that response structure. If you could get the API to return an actual array, you could avoid all the hassle.

To sum up (or tl;dr):

  1. Set up a separate MessageList resource
  2. Set up separate schemas for list / single record as laid above
  3. Use Get record to fetch data
  4. Use a formula to map the returned dictionary object into an actual list of messages
  5. Use the formula output in place of the normal response (store in a variable, display, …)

Hi, we have tried to replicate your issue and we feel like we have tried every possible option. Here’s our setup:



Alerted response is: [{"id":"-MkTCfwftEz36z6wFN6F","message":"text1hgrropehaha","route_ref":"NAPVIE","sender_uid":""},{"id":"-MkTChCz6x7PloFGl0_p","message":"text2Ibelieve","route_ref":"NAPVIE","sender_uid":""},{"id":"-MkTChEZqj1VRn8bB4ls","message":"jwegidjsdnvgdsjvsdnvsjvndsjvkdsnvs","route_ref":"NAPVIE","sender_uid":""},{"id":"-MkhYqvMeTgBdEQsJfJf","message":"text","route_ref":"NAPVIE","sender_uid":"uid"}]

Could you tell us, what we are doing wrong, please? It really tests our sanity. Thanks

Looking at the alert output, the data should be in a workable format now.

[{
  "id":"-MkTCfwftEz36z6wFN6F",
  "message":"text1hgrropehaha",
  "route_ref":"NAPVIE",
  "sender_uid":""
}, ...]

So you got a list of objects with those properties. You should be able to store the output in a variable that has that schema.

Is the set data variable + repeat not working? The List type is not assignable to object type would suggest that data.realtime_chat1 is not a collection or doesn’t have the proper schema. Could you try setting up a page variable, storing the output there and repeating over that?

Hi, Been 2 days, couldn’t get past this. Could you please let me know if you have resolved this?