Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

226
Views
How can a user's displayName be added to a Firestore database collection entry?

I am setting up a simple webpage with Firebase Authentication and a Firestore database which takes user inputs from a form, adds the inputs as a collection document in Firestore, and then also outputs the whole collection. Each document in the collection has two fields, the Name and the Body of the document. The goal is to allow users to make posts on the website using the input form. Everything I described is working, but now I would like to display the user.displayName with the post, to show who exactly created the user input, and that's what I can't figure out how to do. Here's the relevant code, from the script.js of the website:

const createForm = document.querySelector('#create-form');
createForm.addEventListener('submit', (e) => {
  e.preventDefault();

  db.collection('forumposts').add({
    Body: createForm['body'].value,
    //the line below is what I cannot figure out how to set up
    Name: string(user.displayName) 
  }).then(() => {
    //reset the form
    createForm.reset();
  }).catch(err => {
    console.log(err.message)
  })
});

I'm still learning about JavaScript, so I apologize if I am missing something obvious. I know the user's displayName (which is collected upon sign up) is being collected properly, as I can log it to the console and it shows up correctly. I just cannot figure out how to then add it as a field in this database collection input. I have tried searching here on SO for related questions, but am only getting questions related to how the user can add a display name on sign-up. I already have the display name, I just need to input it into the database. Any help would be greatly appreciated!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Where is the "user" defined? That could be null if no user is logged in so it's better to check for the user.

const createForm = document.querySelector('#create-form');
createForm.addEventListener('submit', (e) => {
  e.preventDefault();

  const user = firebase.auth().currentUser

  if (user) {
    db.collection('forumposts').add({
      Body: createForm['body'].value,
      Name: user.displayName || "No username"
    }).then(() => {
      //reset the form
      createForm.reset();
    }).catch(err => {
      console.log(err.message)
    })
  } else {
    console.log("NO user logged in")
  }
});

You don't need the String() constructor. If displayName is defined it'll be a string. You can use if (user.displayName) before adding the post to check if user has a display name.

about 4 years ago · Juan Pablo Isaza Report

0

It's String(), not string(). As long as displayName is a property of user, it will be added to the database.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!