Creating a custom ID for a Firebase POST request

Is it possible to assign a custom ID in Firebase when using the POST method? For example:

{
  "users": {
    "user_one": {
      "username": "jdoe",
      "password": "123"
    }
  }
}

I'm currently working on a Vue.js project and I want to save information about students and their parents like this:

let parent = {
  name: this.parent.name,
  surname: this.parent.surname,
  id_number: this.parent.id_number,
  nationality: this.parent.nationality,
  marital_status: this.parent.marital_status,
  education_level: this.parent.education_level,
  profession: this.parent.profession,
  works: this.parent.works,
  work_location: this.parent.work_location,
  work_days: this.parent.work_days,
  work_phone: this.parent.work_phone,
  home_phone: this.parent.home_phone,
  mobile_phone: this.parent.mobile_phone
}

axios.post('https://projectname.firebaseio.com/parents.json', parent)
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

Answer №1

Make sure to always use the PUT request instead of POST, otherwise you may end up with an auto-generated ID inside your "user_one".

POST request result:

{
  "users": {
    "user_one": {
      "134134hnj34nuj134bn": {
        "username": "jdoe",
        "password": "123"
      }
    }
  }
}

axios.put('https://projectname.firebaseio.com/padres/user_one.json', padre)

Using PUT will give you the desired outcome.

{
  "users": {
    "user_one": {
      "username": "jdoe",
      "password": "123"
    }
  }
}

Answer №2

A solution is readily available by utilizing the HTTP PUT method and indicating the complete path. It could be as straightforward as this, depending on the library you are using:

axios.post('https://projectname.firebaseio.com/padres/user_one.json', padre)

To delve deeper into this topic, refer to the detailed guidance provided in the Firebase REST API documentation regarding the PUT directive.

Answer №3

When utilizing Php cURL, I successfully implemented a PUT request by including my custom ID in the URL.

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

Troubleshooting the Vue.js component rendering issue

I am trying to display only one object from the data on firebase using [objectNumber]. I want to show {{ligler[1].name}} in the template, but it is causing errors: Error when rendering component Uncaught TypeError: Cannot read property 'name' o ...

Simple steps to trigger a PHP page from a JavaScript page by utilizing express functions

Currently, I am attempting to showcase a PHP page using JavaScript with express functions. Here is an illustration of the code: var express = require('express') var app = express() app.get('/', function (req, res) { res.send(' ...

What could be the reason for the defaultCommandTimeout not functioning as expected in my script

Is there a way to wait for only one particular element in Cypress without having to add wait commands everywhere in the test framework? I've come across the solution of adding defaultCommandTimeout in the cypress.json file, but I don't want it t ...

Control the frequency of server requests within a set limit

Currently, I am utilizing the request-sync library to fetch data from a specific site's API. This is how my code looks: let req = request('GET', LINK, { 'headers': { 'Accept' ...

What is the best approach for managing _app.js props when transitioning from a page router to an app router?

Recently, in the latest version of next.js 13.4, the app router has been upgraded to stable. This update prompted me to transition my existing page router to utilize the app router. In _app.jsx file, it is expected to receive Component and pageProps as pr ...

Received a function instead of an array when passing it into a Vue.js component

I'm encountering an issue where I am trying to pass a function into my component and receiving an error message stating: "Invalid prop: type check failed for prop 'form_type'. Expected Array, got Function." The function in question, named se ...

Modifying an object using setState in React (solidity event filter)

Is it possible to update an object's properties with setState in React? For example: this.state = { audit: { name: "1", age: 1 }, } I can log the event to the console using:- myContract.once('MyEvent', { filter: {myIndexedParam: ...

Show various attachment file names using jQuery

Utilizing jQuery, I've implemented a script to extract the filename from a hidden field and then append it to the filename class in my HTML. filenameCache = $('#example-marketing-material-file-cache').val().replace(/^.*[\\\/ ...

Exploring the differences between UTC and non-UTC date formats in Javascript

When working with JavaScript, I encountered a challenge in comparing two dates that are formatted differently. Specifically: 2015-09-30T00:00:00 and 9/30/2015 12:00:00 AM The former is in UTC format while the latter is not. Despite referring to the same ...

Is there a way to handle templates in AngularJS that is reminiscent of Handlebars?

Is there a way to handle an AngularJS template using a syntax similar to Handlebar? <script type="text/ng-template" id="mytemplate"> Name is {{name}} </script> I know how to retrieve the template using $templateCache.get('mytemplate&ap ...

Unable to generate a fresh database entry through form submission

I have designed User and Pairings models as shown below: class User < ActiveRecord::Base enum role: [:student, :supervisor, :admin] has_many :students, class_name: "User", foreign_key: "supervisor_id" belongs_to :supervisor, ...

Issue with iPhone CSS displaying differently on mobile devices

The CSS design does not display correctly on iPhone devices in real-life testing, even though it appears fine on browsers with mobile view emulators. Interestingly, the design also looks great on Android phones but encounters issues specifically on iPhones ...

Updating multiple nodes in a Firebase database can be achieved by utilizing the `then`

When dealing with denormalized data in Firebase, updating multiple nodes can become cumbersome. Each update operation must wait for the previous one to succeed. The current approach shown below is not very readable and can get even more confusing as more ...

What is the top choice for creating a cutting-edge front-end web module?

Which generator or scaffold is recommended for creating a contemporary front-end web module using npm/webpack? ...

Tips for preventing a React component from re-fetching data when navigating back using the browser button

In my React app using Next.js and the Next Link for routing, I have two pages set up: /product-list?year=2020 and /product-list/details?year=2020&month=4 Within the pages/product-list.js file, I am utilizing React router to grab the query parameter ye ...

How can Angular incorporate JSON array values into the current scope?

I am currently working on pushing JSON data into the scope to add to a list without reloading the page. I am using the Ionic framework and infinite scroll feature. Can someone please point out what I am doing wrong and help me figure out how to append new ...

Using JavaScript to retrieve data from a JSON file and showcase it on a jQuery mobile webpage

I am struggling to retrieve JSON data from a PHP URL using JavaScript and display it within a JQuery mobile "li" tag as a category list. Despite spending the last 8 hours on it, I can't seem to get it working using the code provided below. Any help wo ...

The functionality to deselect multiple options in a select box is not functioning properly

There seems to be an issue with removing the selected attribute from the selected values in a jQuery multiselect box. The console is not showing any errors. You can view a working example here The problem lies in this code snippet: $("#mltyslct option ...

Exploring the intersection of Nuxt and TypeScript with the powerful `defineProps`

I am currently utilizing the Nuxt 3 / Vue 3 defineProps in conjunction with TypeScript and I am looking to automatically deduce prop types from TypeScript types. import { User } from '~/types/types' const props = defineProps({ users: { typ ...

Variables are losing their way in the vast expanse of self-submitting

I have a form that needs to be submitted on the same page (index.php) and I want to capture the entered information as a JavaScript variable. <?php $loginid = $_POST[username] . $_POST[password]; ?> Here is some basic code: <script> var pass ...