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?