最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get firebase id - Stack Overflow

programmeradmin5浏览0评论

Anyone know how to get the Firebase unique id? I've tried name(), name, key, key(). Nothing works.

I am able to see the data but I have no idea how to get the id back. I need it.

//Create new customers into firebase 
function saveCustomer(email) {

  firebase.database().ref('/customers').push({
    email: email
  });

  firebase.database().ref('/customers').on("value", function(snapshot) {
    console.log(snapshot.val());
    console.log(snapshot.value.name());
  }, function(errorObject) {
    console.log("The read failed: " + errorObject.code);
  });


}

Anyone know how to get the Firebase unique id? I've tried name(), name, key, key(). Nothing works.

I am able to see the data but I have no idea how to get the id back. I need it.

//Create new customers into firebase 
function saveCustomer(email) {

  firebase.database().ref('/customers').push({
    email: email
  });

  firebase.database().ref('/customers').on("value", function(snapshot) {
    console.log(snapshot.val());
    console.log(snapshot.value.name());
  }, function(errorObject) {
    console.log("The read failed: " + errorObject.code);
  });


}
Share Improve this question edited Aug 14, 2016 at 15:11 Frank van Puffelen 599k84 gold badges888 silver badges858 bronze badges asked Aug 14, 2016 at 5:55 user3183411user3183411 3352 gold badges7 silver badges22 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 11

The call to push will return a Firebase reference. If you are using the Firebase 3 API, you can obtain the unique key of the pushed data from the reference's key property:

var pushedRef = firebase.database().ref('/customers').push({ email: email });
console.log(pushedRef.key);

The key for the pushed data is generated on the client - using a timestamp and random data - and is available immediately.

Calling push() will return a reference to the new data path, which you can use to get the value of its ID or set data to it.

The following code will result in the same data as the above example, but now we'll have access to the unique push ID that was generated:

// Generate a reference to a new location and add some data using push()
var newPostRef = postsRef.push();
// Get the unique ID generated by push()
var postID = newPostRef.key();

Documentation.

but this method won't work when you also need the id beforehand

  • for example to save it in the database itself.

Firebase suggests this:

// Add a new document with a generated id. 
var newCityRef = db.collection("cities").doc(); 

--for some reason, push() and key() didn't work for me. also in this case the reference contains the whole path. so need a different method for getting the id.

Doing this below helped to get the id from the reference and use it.

const ref = db.collection('projects').doc()
console.log(ref.id)  // prints the unique id
ref.set({id: ref.id})  // sets the contents of the doc using the id
.then(() => {  // fetch the doc again and show its data
    ref.get().then(doc => {
        console.log(doc.data())  // prints {id: "the unique id"}
    })
})
发布评论

评论列表(0)

  1. 暂无评论