Is your Cloud Functions task generating an Array when querying?

To access items and products in my database, I need to retrieve the "ean" field from the product and check if it matches the one in the request body.

The structure of my database is as follows:

"cart": {
  "items": {
    "0": {info here},
    "1": {info here}
    "2": {
      "more info here",
      "product": {
         "available": true"
         "quantity": 231,
         "ean": "0000001312"
      }
    continue listing until 47

However, when I run my cloud function:

exports.getItemByEan = functions.https.onRequest(async (request, response) => {
  const db = admin.database();
  const items = db.ref();
  const eanRef = items.child('cart').child('items');
  const query = eanRef.orderByKey();

  try {
    const dataSnapshot = await eanRef.once('value');
    response.send(dataSnapshot.val());
  } catch (error) {
    console.log(error)
  }
  }) 

});

I'm having trouble retrieving the specific product by its Ean code. The result seems to be an array instead of an object, and the keys aren't appearing as expected:

[
    {,
        "product": {
            "available": true,
            "quantity": 9183
            "ean": "0000000000017",
    },
    {

   "product": {
            "available": true,
            "quantity": 131
            "ean": "0000000044790",
        },
    },
    .....continues

I want to understand why the query result is an array instead of an object with keys like in the Firebase Database. Additionally, I'm looking for a better way to find a specific product by its Ean code.

For example, when I try:

const db = admin.database();
const items = db.ref();
const eanRef = items.child('cart').child('items').child('product'); // This doesn't work, it returns nothing. Why? Isn't the "product" field inside the item?

Answer №1

In cases where the database keys are exclusively numeric, the data structure returned will be an array rather than an object. The elements within the array will correspond directly to the keys of the nodes in the database. It is essential for your code to account for this situation and handle it accordingly. If you require the client to receive an object instead of an array, you must perform the conversion manually.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Display HTML instead of text in print mode

Hello, I need help with printing HTML code, specifically an iframe. When I try to add my HTML iframe code, it only prints as plain text. I want to be able to see the actual iframe with its content displayed. Thank you. <script> const messages = [&apo ...

The mystery of why gulp-watch fails to remove files

I need help figuring out why my gulp-watch task isn't deleting files from the /dest directory when I delete them from /src. Can someone spot the issue? var watch = require('gulp-watch'); var imagemin = require('gulp-imagemin'); ...

Encountering a console error: Prop type validation failed for the `Rating` component with the message that the prop `value` is required but is currently `undefined`

I am encountering a proptype error which is causing an issue with the URL display on my Chrome browser. Instead of showing a proper address, I am seeing the URL as undefined like this: http://localhost:3000/order/undefined Instead of undefined, I should h ...

Having Difficulty Converting JavaScript Objects/JSON into PHP Arrays

This particular inquiry has no relation to the previously mentioned identical answer/question... In JavaScript, I am dealing with a substantial list of over 1,000 items displayed in this format... var plugins = [ { name: "Roundabout - Interac ...

The Evolution of Bulma's Navigation Menu

Creating a transparent menu in Bulma has been successful for the desktop viewport: VIEW DESKTOP MENU However, when attempting to implement the same design on mobile, the menu ends up like this: VIEW MOBILE/TABLET MENU The mobile version seems to inheri ...

What sets Observables (Rx.js) apart from ES2015 generators?

From what I've gathered, there are various techniques used for solving asynchronous programming workflows: Callbacks (CSP) Promises Newer methods include: Rx.js Observables (or mostjs, bacon.js, xstream etc) ES6 generators Async/Await The trend ...

How can I rename attribute values in a dropdown menu and ensure they work properly?

I'm facing an issue with my code. How can I store the option values in a database when they have numbering like value="1" or value="2"? Can I change it to something like value="1000" and have the other select box change to value="250" when selected? ...

Setting up Type ORM configuration with a .env file in Nest.JS: A step-by-step guide

I encountered an issue while attempting to configure TypeORM with a .env file and run migration script. Here's what I tried: 1. Added scripts to package.json "migration:generate": "node_modules/.bin/typeorm migration:generate -n", "migratio ...

Implementing a class addition on focus event using Angular 2

Currently, I am in the process of upgrading an Angular 1 application to Angular 2 and encountering an issue with one of my existing directives. The task at hand is straightforward. When an input field is focused, a class should be added (md-input-focus) a ...

Tips for troubleshooting and resolving the npm ERR! code E404 issue in Vue.js when implementing a datepicker

I am a newcomer to Vue.js and I am currently exploring the use of a datepicker from However, upon importing the script for usage, I encountered the following message: 'VMdDateRangePicker' is declared but its value is never read.ts(6133) Cou ...

How to align an image in the center of a circular flex container

I'm facing an issue in my Angular project where I have the following code snippet: onChange(event: any) { var reader = new FileReader(); reader.onload = (event: any) => { this.url = event.target.result; }; reader.readAsData ...

Viewing a document generated using the Google Drive API using the Google Drive Viewer

Using the Google Drive API, I am able to generate a document and receive some URLs: alternateLink : https://docs.google.com/document/d/xxxsome_idxxxxx/edit?usp=drivesdk embedLink https://docs.goo ...

Getting the value of a hidden input field within a div by accessing the event.target that initiated the event in JavaScript

I am working with a set of li elements, each containing specific child elements structured like this: <li class="list"> <!--Parent--> <input type="hidden" id="id" value="3"> <!--Child 1--> <div class="cd-1">....</div ...

Is there a way to seamlessly integrate typeahead.js with jquery.validate?

Currently, I have a website built on ASP.NET MVC 5 which utilizes jQuery validation (specifically 'jquery.validate.js' in the MVC project template). My goal is to implement type-ahead functionality using 'typeahead.js' on an input field ...

Learn how to retrieve information using the dash operator in ReactJS

I find it a bit amusing, but I'm encountering an issue. Can anyone lend me a hand with this? So, I'm using an API to fetch some data and it appears that the response from the API is in this format: start-time:2323232 end-time:2332323 Now, when I ...

Having trouble in React.js when trying to run `npm start` with an

Upon initially building a todo app in react.js by using the command: npx create-react-app app_name When I proceeded to run the command npm start, it resulted in displaying errors: In further investigation, I discovered a log file with various lines that ...

Extract the list of selected item Id's from the HTML table and transfer them to the Controller

I have a table in my ASP.NET MVC project that displays information about file types and IDs (which are hidden) to the user. When the user selects checkboxes and clicks on the "Send Request Mail" button, I need to retrieve the IDs of the selected rows and ...

Is it feasible to develop a Grafana datasource plugin that does not rely on an external backend system?

I am in the process of developing a Grafana datasource plugin that operates independently without relying on an external backend. My plugin is based on the simple-json datasource plugin available at: https://github.com/grafana/simple-json-datasource In a ...

What steps should I take to stop material-ui Select options from opening when clicking on specific parts of the selected option?

Presently, I am utilizing a Select component from @material-ui/core/Select, which contains only one option for simplification purposes, and the code snippet is as follows: <FormControl> <InputLabel id="demo-controlled-open-select-label">Test ...

Unable to locate the tag using .find() function following the use of .attr() method

I am currently utilizing jQuery in conjunction with node-horseman to interact with a specific page. My approach involves referencing the page's data-id attribute, which I then pass to my application to search for the li element containing this attribu ...