2016-11-11 50 views
0

每当我将数据推送到firebase时,我都想命名自己的密钥。命名我自己的Firebase唯一ID /密钥。 Javascript

firebase.initializeApp(config); 

var database = firebase.database().ref('players'); 
var theKey; 
var newPostRef; 

$(document).on('click', '#enter', getName); 

function getName() 
{ 
    var name = $('#user').val().trim(); 

    database.push({ 
     name: name 
    }); 

} 

database.on("child_added", function(childSnapshot){ 
    var childKey = childSnapshot.key; 
    var childData = childSnapshot.val; 
    console.log('key', childKey) 
    console.log('data', childData) 

    }, function (errorObject) { 

     console.log("The read failed: " + errorObject.code); 

    }) 

的的console.log(“钥匙”,childKey)将显示随机生成的密钥通常是随机字母的长字符串。我如何命名我自己的密钥?

+0

为什么你想自己的钥匙? –

回答

1

而不是使用push()函数使用集。

例如:

usersRef.child("custom-column-name").set({ 
     date_of_birth: "June 23, 1912", 
     full_name: "Alan Turing" 
}); 
+0

这不起作用 –