What is the best way to retrieve different data from the Firebase database within a cloud function?

Currently, I am utilizing the Firebase real-time database and attempting to establish a trigger for when a field named ignore is added to my DB. However, before proceeding with this trigger, I need to access another set of data within the database in order to incorporate it into my function. The function I have written looks something like this:

exports.dbTest2 = functions.database.ref('/{uid}/ignore')
  .onCreate((snapshot, context) => {

    const uid = context.params.uid
    console.log(`Current User ${uid}`)

    // Grasping the newly added data from the specified location
    const ignoreData = snapshot.val()
    const endTime = new Date(ignoreData.endTime).toString()

    const scheduleRef = snapshot.ref.parent.child('schedule')
    console.log(scheduleRef)

    let scheduleArr = [ /*something*/ ]
    return snapshot.ref.parent.child('schedule').set(scheduleArr)

  });

Here's how my database structure appears:

https://i.sstatic.net/iOaNE.png

From the reference path {uid}/ignore, my objective is to obtain the first element present at the schedule reference (highlighted in the image provided). While debugging the code above, I attempted to log the reference but couldn't find a direct way to retrieve the value from that object.

Is there any approach to accessing the object highlighted in the image within my cloud function?

Answer №1

Consider trying a different approach by nesting functions.database.ref as shown below:

exports.dbTest3 = functions.database.ref('/{userId}/data')
    .onCreate((snapshotData, context) => {

      const userId = context.params.userId;

      admin.database().ref(`/${userId}/info`).once('value', snapshotInfo => {
        console.log(snapshotInfo.val()[0]);
      });
    });

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

String casting for large JavaScript integers may require rounding to function properly

When trying to pass a large integer to a function from an onclick event in HTML, I always encounter issues with rounding. Despite using bigInt libraries, I still struggle to pass the full number accurately and would prefer a simple string casting method. ...

Mysterious element materializing within the code featuring the attribute "data-original-title"

I've noticed a mysterious div that keeps popping up in the code of the website I'm currently working on. It seems like countless developers have tinkered with this project over time. This page is utilizing Bootstrap and is based on a .Net forms ...

AngularJS does not allow data to be deleted in mongodb

Backend code: var User = require("./models/user"); var express = require('express'), app = express(), Account = require("./models/account"), mongoose = require('mongoose'), passport = require("passport"), basicAuth = require('basi ...

Creating a JSON hierarchy from an adjacency list

I am currently working with adjacency data that includes ID's and Parent ID's. My goal is to convert this data into hierarchical data by creating nested JSON structures. While I have managed to make it work, I encountered an issue when dealing ...

Why is it necessary to use 'then' on the response JSON when using the Fetch API? It's like trying to decipher the hidden meaning

While delving into the realm of promises, I decided to test it out with a basic GET request on Twitch. Yet, one aspect is still puzzling me - why does json() return a promise? The response already contains the data, so what's the deal with it being wr ...

Why am I receiving the error message "Argument of type 'number' is not assignable to parameter of type 'never'?"

import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { showSecret = false; logArr ...

The function will not be triggered when the form is submitted

I've set up this form to send data to the server-side. It's built in HTML/CSS with AngularJS as well. I made sure that the button is placed inside the form, a common mistake that can cause issues. However, despite my efforts, the function "onAddE ...

The event was triggered, however, some sections of the code did not run

I am currently working on a project called lan-info on GitHub. Here is the code snippet that I am using: let arpSweepCommand = './arpSweep.sh'; app.get('/arp', (req, res) => { console.log('we have a working signal!'); ...

React click event not being applied to the surrounding elements

Within my React component, I have the following structure: <div class="iconButton" onClick={clickEvent}> <MyIcon /> </div> The MyIcon component utilizes an SVG file and displays it on the screen. A hover effect is tri ...

How do popular social media platforms such as Facebook and Twitter utilize real-time APIs to display live updates in news feeds

I'm curious about the technology being used for real-time updates, and I doubt they rely on AJax methods like setInterval() to make server requests every second. This approach could be inefficient with a large number of concurrent users. Do you think ...

Having trouble muting the audio on my Vue audio player

I'm facing some challenges with muting the audio within my vue app. I have a list of songs that can be played, paused, shuffled, etc., but I can't seem to get the mute function working. Here's what I have in the JavaScript: mute() ...

What is the best way to select specific points from a THREE.Points object?

I am working on a single THREE.Points() representing a point cloud and I am trying to select individual points using mouse clicks. starsGeometry = new THREE.Geometry(); for ( var i = 0; i < 10000; i ++ ) { var star = new THREE.Vector3( ...

Understanding the sequence of operations in Javascript using setTimeout()

If I have the following code: function testA { setTimeout('testB()', 1000); doLong(); } function testB { doSomething(); } function doLong() { //takes a few seconds to do something } When I run testA(), what happens after 1000 mill ...

Is the code for uploading multiple images not functioning as expected?

My Photo Table is Column Name Data Type Constraint PhotoID Int Primary key,auto increment PhotoName Varchar(100) ExtName Varchar(100) PhotoType Varchar(100) PhotoSize Int TempleID Int Foreign key with templ ...

The flow of Ajax execution halts once the initial calculation is completed

I have been developing an AJAX code that calculates fees. The code works well initially, but it stops functioning when I try to perform a second operation. <script> var weight = document.getElementById("weight").value; var ship_type = document.g ...

Tips for Deleting Navigation History in Nativescript-Vue Manual Routing

I find myself in a situation where the navigation calls are stacking up continuously, as illustrated in the image below. https://i.sstatic.net/evlFx.png This poses a problem, especially when I try to logout as it only adds another entry to the navigation ...

Leveraging the power of the Google API within the Dojo module

Is it possible to integrate Google Maps API within a Dojo Toolkit module in the following way? define(["dojo/dom"], function (dom) { var input = dom.byId("searchBox"); var autocomplete = new google.maps.places.Autocomplete(input, options); } ...

Avoiding the resizing of table headers when positioned at the top of a window

Having an issue with resizing elements on a webpage. I have dynamically generated a table from JSON data and have a function that sticks the table header to the top of the page when scrolling: var header = $("#dataheader").offset(); $(window).scroll(func ...

Implementing Material-UI’s FlatButton and Dialog in ReactJS for dynamic TableRow functionality

I am working with Material-UI and have implemented a <Table> component. Each dynamically rendered <TableRow> in the <TableBody> needs to include a button (<FlatButton>) within one of the columns. When this button is clicked, a <D ...

How to send emails with attachments using WordPress?

Looking for assistance in sending emails with attachments using Wordpress. I am struggling and believe there might be something missing in my code. Any help would be greatly appreciated! :) Below is the code spread across two files: Name (required)<b ...