I am looking to store a collection of objects in Firebase using a single request, and I want Firebase to generate a unique key for each object without using array

I am looking to store a set of objects in Firebase using a single request with a unique key generated by Firebase (without using array indexes as keys).

let object_list =  {
  '0': {
    'title':'title 1',
    'time':'12.30pm',
  },
  '1': {
    'title':'title 2',
    'time':'12.30pm',
  },
  '2': {
    'title':'title 3',
    'time':'12.30pm',
  },
}

this.agendaRef = firebase.database().ref('objs/');
let data = this.agendaRef.push();
data.set(object_list);

The expected output should be

 -LioB1b06T7tfVhbJmss6o
    time: "12.30pm"
    title:"title 1"

 -LioB1b06T7tfVhbsdfsdf
    time: "12.30pm"
    title:"title 2"

 -LioB1b06Tsdsdsd7tfVh2
    time: "12.30pm"
    title: "title 3"

However, the actual output is

 -LioB1b06T7tfVhbJm6o
 0
    time: "12.30pm"
    title:"title 1"
 1
    time: "12.30pm"
    title:"title 2"
 2
    time: "12.30pm"
    title: "title 3"

Answer №1

Every time the push function is called, a unique ID is generated. Therefore, if you need multiple IDs, you must call push multiple times.

An important point to note is that invoking push() without any arguments only affects the client side. This means that you can call push() three times to generate the IDs and still execute just one write operation, similar to the example below:

let object_list =  [
  {
    'title':'title 1',
    'time':'12.30pm',
  },
  {
    'title':'title 2',
    'time':'12.30pm',
  },
  {
    'title':'title 3',
    'time':'12.30pm',
  }
}]

this.agendaRef = firebase.database().ref('objs/');

let object = {};
object_list.forEach((value) => {
  object[this.agendaRef.push().key] = value;
})

data.set(object);

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 is the best way to include text or a label on my Angular map without using a marker?

I am currently using the agm-map module in my angular project. I want to place a label or text at the center of a polygon on the map without using markers. How can I achieve this functionality in Typescript? I attempted to use the MapLabel Utility for thi ...

Acquire keys from a different residence

Currently, I am working on a React component that accepts data through props and I aim to implement safe property access. I have experimented with the following: type Props = { items?: any[]; // uncertain about using type "any" bindValue?: keyof Prop ...

What is the most straightforward way to make a property observable in terms of syntax?

There are countless tutorials out there demonstrating various ways to implement observables in Angular, but many of them are too complex for my needs. Some are outdated and no longer applicable. Let's assume I have a service with a single property ca ...

sending information to ng-content from mother in Angular

In the container-page.component.ts, I have set up the following structure using the ngrx approach. <parent-comp><child-comp [someInput]="someValue"></child-comp></parent-comp> Within the template of parent-comp, there is: <div ...

Is it possible to generate an array of strings from the keys of a type or interface?

Imagine a scenario where we have a type or interface defined as NumberLookupCriteria: type NumberLookupCriteria = { dialCode: string; phoneNumber: string; } or interface NumberLookupCriteria { dialCode: string; phoneNumber: string; } Is there a w ...

Stop receiving notifications for channel events in Stream with the GetStream.io Chat API

I'm having trouble understanding how to stop listening for channel events in JavaScript using the Stream Chat API. According to the documentation: // remove the handler for all "message.new" events on the channel channel.off("message.new", myChanne ...

Build a unique array of identifiers extracted from an object

https://i.sstatic.net/PaFXj.png I am seeking advice on how to extract an array of IDs values by iterating through an object in React JS. https://i.sstatic.net/GV6ga.png const initialState = useMemo(()=> { return dataTable.filter(result => f ...

How to detect changes in Angular2 forms

Exploring Angular2 4.0, I've created a FormGroup structured as follows: this.form = this._fb.group({ a: ['', [Validators.required]], b: ['', [Validators.required]], c: ['', [Validators.required]], ...

Utilizing PrimeNG checkbox groups in a reactive form: A guide to retrieving all selected values

I am facing an issue with a group of <p-checkbox> elements from PrimeNG. They all have the same name and formControlName attributes. The form control's value is an array, but it seems to only retain the selection of the last checkbox clicked. T ...

Encountering an error in resolving symbol values statically within the Angular module

Following a helpful guide, I have created the module below: @NgModule({ // ... }) export class MatchMediaModule { private static forRootHasAlreadyBeenCalled: boolean = false; // This method ensures that the providers of the feature module ar ...

Difficulty in HttpRequest handling between Angular and .Net

My http response seems to be causing some trouble, and I'm completely stumped on what the issue might be. Can someone provide assistance, please? import { Component, OnInit } from '@angular/core'; import {SharedService} from 'src/app/sh ...

The issue with Angular 5 Interceptor failing to log out upon a server call error

Source : https://gist.github.com/tobbbe/08e01db92c32346b226333a3f952df22 intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { return next.handle(this.addToken(request)) .pipe( ...

Unable to inject basic service into component

Despite all my efforts, I am struggling to inject a simple service into an Angular2 component. Everything is transpiling correctly, but I keep encountering this error: EXCEPTION: TypeError: Cannot read property 'getSurveyItem' of undefined Even ...

Receiving the [object HTMLInputElement] on the screen rather than a numerical value

I have an input box where a user can enter a number. When they click a button, I want that number to be displayed on the page. However, instead of seeing the number, I am getting the output as [object HTMLInputElement]. Below is my TypeScript code: let qu ...

What is the best way to tally up the occurrences of a specific class within an Angular application?

After reviewing the resources provided below on impure and pure pipes in Angular applications: What is impure pipe in Angular? I have been curious about inspecting the instances created by an Angular application firsthand, although I am uncertain if thi ...

Setting attributes within an object by looping through its keys

I define an enum called REPORT_PARAMETERS: enum REPORT_PARAMETERS { DEFECT_CODE = 'DEFECT_CODE', ORGANIZATION = 'ORGANIZATION' } In addition, I have a Form interface and two objects - form and formMappers that utilize the REPOR ...

Why won't T.chain chain properly in Effect-ts?

I have a simple program that I've been working on: import * as T from "@effect-ts/core/Effect"; import { pipe } from "@effect-ts/core/Function"; import { tag } from "@effect-ts/core/Has"; interface ConsoleModule { log: ...

Best practice for setting up components in Angular 2 using HTML

I have developed a component that relies on external parameters to determine its properties: import { Component, Input } from '@angular/core'; import { NavController } from 'ionic-angular'; /* Info card for displaying informatio ...

Guide to setting up Firebase pagination in a NextJS 13 server component

Currently, I am working on developing a product page that showcases all products and functions as a server component. The challenge I am facing is the inability to pass the last visible document snapshot required by the startAfter() query. Below is the fu ...

When attempting to print a Rectangle on my webpage using JavaScript and taking user input, I encountered an error stating that 'str' is not defined

I'm trying to implement a feature where clicking on the "try it" button will prompt the user for the number of lines and then print that many lines in the form of a rectangle. However, when I run my code, nothing appears on the DOM and there is an err ...