Robin Singh
Robin Singh

Reputation: 1796

How can i save data in firebase without unique ID?

I am saving my data in firebase database. But the issue is data saved with auto-increment ID. But i need custom ID. Please guide me how can i achieve this. Below is the output and code. Please check and guide me what can i do.

Data comes like this

enter image description here

I want to display Job1, Job2, Job3..... and so on.

Code

router.get("/firebase/save", (req, res) => {
  const ref = firebase.app().database().ref(`levela_jobs`);
  ref.remove();
  levelConnection.getConnection((err, conn) => {
    if (err) throw err;
    getJobs(conn)
      .then((result) => {
        result.forEach((job) => {
          var dt = dateTime.create(job.job_created_at);
          var formatted = dt.format("f d Y");
          const data = {
            applications_count: job.count,
            job_title: job.job_title,
            job_description: stripTags(job.job_description),
            created_at: formatted,
          };
          ref.child(job.job_city).push(data);
        });
        res.send({ message: "Data saved successfully" }).status(200);
      })
      .catch((error) => {
        res.send(error);
      });
  });
});

Any solution appreciated!

Upvotes: 0

Views: 799

Answers (1)

Azizur Rehman
Azizur Rehman

Reputation: 2113

push() generates a unique id when inserting, use set() instead

as per your case,

use an additional child For eg.

 ref.child(job.job_city).child(myId).set(data);

Upvotes: 1

Related Questions