以下代码使我可以返回当前用户的所有表达式。 在此示例中,控制台中显示以下表达式:
但是我不确定如何为当前用户在“表达式”下列出所有标题。 理想的结果应该是: KIKKER,CPM2MAN
function f_returnUserDetails(a){
var key;
var childData;
return new Promise(function(resolve, reject) { //return promise
firebase.database().ref('/users/').orderByChild("uid").equalTo(a).on('value', function (snapshot) {
snapshot.forEach(function(childSnapshot) {
key = childSnapshot.key;
childData = childSnapshot.val();
resolve([childData, childSnapshot.key]);
});
});
});
};
firebase.auth().onAuthStateChanged(function(user){
var user = firebase.auth().currentUser;
var uid = user.uid;
f_returnUserDetails(uid).then((dbItems) => {
console.log(dbItems[0].Expressions);
});
});
答案 0 :(得分:2)
通过执行以下操作,您应该获得所需的值:
firebase.database().ref('/users/').orderByChild("uid").equalTo(a).on('value', function (snapshot) {
snapshot.forEach(function (childSnapshot) {
key = childSnapshot.key;
childData = childSnapshot.val();
for (v of Object.values(childData.Expressions)) {
if (v.hasOwnProperty('title')) {
console.log(v.title);
}
}
});
});
答案 1 :(得分:0)
您必须构建一个表达式数组并将其返回到函数中
function f_returnUserDetails(a){
return firebase.database().ref('/users/' + a + '/Expressions').once('value')
.then(snapshot => {
let expressions = [];
snapshot.forEach(childSnapshot =>{
let expression = childSnapshot.val();
expression.id = childSnapshot.key;
expressions.push(expression);
});
return expressions;
});
}
稍后您可以使用此功能
f_returnUserDetails(uid).then((dbItems) => {
console.log(dbItems[0].title);
});