我不知道如何获取唯一键中的数据(“age”)(由push()生成)
这是我的代码
var dbRef = firebase.database().ref('test');
var a = dbRef.push().set({name: "apple", age: "12", sex: "male"});
dbRef.on('value', snap => {
console.log(snap.val()); //I want to get age: "12" !!
});
这是输出(来自控制台)
Object { -Kfq7woK-DmUsZ728lgF: Object }
答案 0 :(得分:1)
var dbRef = firebase.database().ref('test');
var a = dbRef.push().set({name: "apple", age: "12", sex: "male"});
dbRef.on('child_added', snap => {
console.log(snap.val()); //I want to get age: "12" !!
console.log(snap.val().age); //I want to get age: "12" !!
});
这就是我想要的答案,谢谢大家!
答案 1 :(得分:0)
console.log(snap.val().age);
通过执行snap.val(),您将获得整个JavaScript对象。只需添加.age即可获得“年龄”属性。
您可以阅读有关对象的一些文档,例如https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects
的https://www.w3schools.com/js/js_objects.asp