[英]How can I get the second user Id in a two user id's in the same field
I have a only two user chat that puts both user id's in a field (to make it a new id).我只有两个用户聊天,将两个用户 ID 放在一个字段中(使其成为新 ID)。 I need to get the second Id and show letter in a list.
我需要获取第二个 ID 并在列表中显示字母。 How can I get the second part of the id after the hyphen
如何在连字符后获得 id 的第二部分
{
"chat" : {
"st98ySAm5CNeq2WVDTCAQJ3ZcpP2-2C95b9MJprgpCITMXknPuXS52mR2" : {
"-M9Qk0mmv-r2C0kaFqpn" : {
"_id" : 1591750954541,
"createdAt" : 1591750954541,
"order" : -1591750954541,
"text" : "Hello there",
"uid" : "2C95b9MJprgpCITMXknPuXS52mR2"
},
"-M9QkBv-rl18Nc4OyfpV" : {
"_id" : 1591751000122,
"createdAt" : 1591751000122,
"order" : -1591751000122,
"text" : "Hello",
"uid" : "st98ySAm5CNeq2WVDTCAQJ3ZcpP2"
}
},
"st98ySAm5CNeq2WVDTCAQJ3ZcpP2-heGJLHnvwMgofczT9PHSlRwANCB2" : {
"-M9_eYajRuO0RjGDJHSM" : {
"_id" : 1591917295564,
"createdAt" : 1591917295564,
"order" : -1591917295564,
"text" : "Mmmm",
"uid" : "heGJLHnvwMgofczT9PHSlRwANCB2"
},
"-M9_efDQ1qHUrLMndU2o" : {
"_id" : 1591917327330,
"createdAt" : 1591917327330,
"order" : -1591917327330,
"text" : "How are you BIG MAN",
"uid" : "st98ySAm5CNeq2WVDTCAQJ3ZcpP2"
}
},
"yKaXQZCbn8RdxO96tsbf9ZQHK3g1-2C95b9MJprgpCITMXknPuXS52mR2" : {
"-M9QUli8eRb7gEEwgIM8" : {
"_id" : 1591746694381,
"createdAt" : 1591746694381,
"order" : -1591746694381,
"text" : "Hello",
"uid" : "2C95b9MJprgpCITMXknPuXS52mR2"
},
"-M9Qji3QtqOKYrRZV7Wg" : {
"_id" : 1591750873750,
"createdAt" : 1591750873750,
"order" : -1591750873750,
"text" : "Hi bro",
"uid" : "yKaXQZCbn8RdxO96tsbf9ZQHK3g1"
},
"-M9QjwXH4diBq0mj_aPz" : {
"_id" : 1591750933005,
"createdAt" : 1591750933005,
"order" : -1591750933005,
"text" : "Hi how are you",
"uid" : "2C95b9MJprgpCITMXknPuXS52mR2"
}
}
}
}
I split the id into to and took the second part but this way it makes multiple arrays each time it loops and and creates some empty objects我将 id 拆分为第二部分,但这样它每次循环时都会产生多个 arrays 并创建一些空对象
componentDidMount() {
firebase
.database()
.ref()
.child("chat")
.on("child_added", snapshot => {
const anotherUsers = [];
let array = snapshot.key.split("-");
let a = array[1];
anotherUsers.push({
key: a
});
this.setState({
unknownUser2: anotherUsers
});
});
}
Try something like this尝试这样的事情
componentDidMount() {
firebase
.database()
.ref()
.child("chat")
.on("child_added", snapshot => {
const unknown = [...this.state.unknownUser2];
let array = snapshot.key.split("-");
let a = array[1];
const updated=[...unKnown,{key: a}]
this.setState({
unknownUser2: updated
});
});
}
To show the second part of each key under chat
, it is probably easier to use a value
event:要在
chat
下显示每个键的第二部分,使用value
事件可能更容易:
firebase
.database()
.ref()
.child("chat")
.on("value", snapshot => {
const anotherUsers = [];
snapshot.forEach((child) => {
const second = child.key.split("-")[1];
anotherUsers.push({ key: second });
});
this.setState({
unknownUser2: anotherUsers
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.