Exploring methods in Firebase Cloud Functions to retrieve and modify values

I'm currently attempting to retrieve a value from a specific location in my database, add 1 to it, and then update it back. However, I keep encountering various errors, with the most recent being:

TypeError: Cannot read property 'update' of undefined
at admin.database.ref.once.then (/user_code/lib/index.js:22:25)

I'm unsure as to why it's interpreting it as a property when I thought it was a method. The entirety of my method looks like this:

admin.database().ref('users/' + senderId + '/contacts/' + recipientId)
.once('value').then((contactSnapshot) => {
    var mContact = contactSnapshot.val()
    let unread = mContact['unread']
    console.log('contact name is ' + mContact['user_name'] + ' unread count is ' + unread)
    unread++
    var contactReference = mContact.ref
    contactReference.update({'unread' : unread})
})

My console log displays the contact name and unread count perfectly fine. I admittedly struggle with JavaScript (which this actually is TypeScript), and I plan on addressing that. Any assistance would be greatly appreciated.

Answer №1

mContact = contactSnapshot.val() retrieves the original javascript value from the specific location in the database that was queried. This value does not include any other related objects. 'mContact' will not include a ref property, unless a key with the name ref exists in the database at that location.

It appears that you may be looking for contactSnapshot.ref instead. Alternatively, you can simply store the reference as you initially defined it:

const contactReference = admin.database().ref('users/' + senderId + '/contacts/' + recipientId)

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

What strategy does Node recommend for organizing code into multiple files?

In the midst of my current project, which involves NodeJS and Typescript, I am developing an HTML5 client that communicates with a NodeJS server via web-sockets. With a background in C#, I prefer to organize my code into separate files for different functi ...

The value from select2 dropdown does not get populated in my article in Angular

I am attempting to link the selected value in a dropdown menu to an article, with a property that matches the type of the dropdown's data source. However, despite logging my article object, the property intended to hold the selected dropdown value app ...

"The act of initializing an EntryComponent in Angular results in the creation of a brand

In my main component, app.component.ts, I have integrated a new service into the providers[] array and initialized it in the constructor: @Component({ selector: 'app-main', templateUrl: './app.component.html', styleUrls: ['. ...

The JSON Parser encountered an error while trying to parse the data: org.json.JSONException: The value null, of type org.json.JSONObject$1, cannot be converted to

Whenever I run the code provided below, it gives me an exception message: "JSON Parser﹕ Error parsing data org.json.JSONException: Value null of type org.json.JSONObject$1 cannot be converted to JSONObject" Is there anyone here who can assist me with ...

Stopping or pausing an HTML5 audio element in Angular with component.ts

Is there a way to create a custom function in my component.ts file to pause an HTML audio element? I can't seem to find any built-in methods for pausing audio when using document.getElement. <audio controls id="audio-file" > <source src="sam ...

The variable 'selectedvalue' is being accessed before it has been initialized

I'm currently working on sharing the date between components using BehaviorSubject, but I'm encountering an error in the process. public data = new BehaviorSubject<any>(this.selectedValue); public sharedData = this.data.asObservable(); sele ...

What is the process of retrieving the JSON array containing data from Reddit?

I am struggling to grasp the idea of JSON arrays and how to pinpoint a specific array from a JSON response. My objective is to retrieve the "URL" key value from the "data" objects within the "children" array at http://www.reddit.com/r/gifs/.json, but I am ...

The sequence of operations when assigning in Typescript with || and utilizing the array .find method

I need to ensure that the operations in my assignment are happening in a specific sequence. As far as I can tell, it should be following the order listed below. However, I have not been able to locate any documentation on TypeScript that definitively confi ...

Is there a bug causing the Android widget to cease updating its code after a few hours

Currently, I am developing a battery widget that receives the Intent.ACTION_BATTERY_CHANGED in the onReceive() method. The widget is able to retrieve data successfully initially, but after running for about 4 to 5 hours, it stops updating. I am using a tex ...

To switch to desktop mode, double click; for mobile view, just tap once

I am looking to implement 2 different gestures for a specific feature in my application. Ideally, I want users to be able to double click on a card to open it in desktop view, but if they are using a phone, a single tap should suffice. How can I achieve th ...

Encountering an error while trying to import JSON in TypeScript

I am in need of using mock JSON data to test the rendering of my front-end. import MOCK_FAQ from '../../mocks/FAQ.json'; However, when attempting to import the file, I encountered this exception: Cannot find module '../../mocks/FAQ.json&a ...

Error: Angular 4 component failed to load

It seems that the route /users is not functioning as expected; instead of loading the UsersComponent, it loads the AppComponent. Why is the /users route not loading the correct component? Here is a snippet from app.module.ts: import { Brows ...

Revamp your search experience with Algolia's Angular Instant Search: Design a personalized search box template

Custom Search Box Request: My goal is to implement an autosuggest search box using Algolia Angular instant search with an Angular Material design. To achieve this, I am planning to customize the search box component by replacing the standard <ais-sea ...

Making @types compatible with TypeScript 2.1 in Visual Studio 2015 Update 3

The potential of TS 2.x @types is intriguing, but I'm struggling to make it work effectively! I'm using Visual Studio 2015 - version 14.0.25431.01 Update 3 My TypeScript version for Visual Studio 2015 is 2.1.4, downloaded from this link The VS ...

Tips on how to effectively unit test error scenarios when creating a DOM element using Angular

I designed a feature to insert a canonical tag. Here is the code for the feature: createLinkForCanonicalURL(tagData) { try { if (!tagData) { return; } const link: HTMLLinkElement = this.dom.createElement('link'); ...

Troubleshooting: Resolving Issues with Android Toast Notifications

Currently, I am in the process of developing a game for Android using Andengine. My project consists of classes such as MainActivity and GameScene. While I have successfully implemented Toast messages in GameActivity, I am facing issues when trying to inco ...

Using injected services within static methods may seem tricky at first, but once you

I am exploring the integration of angularjs and typescript in my project. Currently, I am working on creating an Orm factory using typescript but have encountered some challenges. The structure of my factory class is as follows: class OrmModel implements ...

What is the proper way to bring in Typescript types from the ebay-api third-party library?

My TypeScript code looks like this: import type { CoreItem } from 'ebay-api'; declare interface Props { item: CoreItem; } export default function Item({ item }: Props) { // <snip> } However, I encounter an issue when trying to build ...

Exploring the intricacies of pattern matching with JavaScript visualization

I am currently working on improving my pattern matching function by creating a visualizer for it. To achieve this, I need to slow down the execution of each comparison. My approach involves declaring the i and j variables outside of the function so that I ...

Modify the data type of an object member based on its original type

I am seeking to convert the data type of each member in an object based on the specific member variable. Here is an example: class A { fct = (): string => 'blabla'; } class B { fct = (): number => 1; } class C { fct = (): { o ...