document.getElementbyId 未找到通过提取添加的元素
document.getElementbyId is not finding element added by a fetch
我有一个聊天程序,可以在收到消息时向 dom 添加一个元素。此元素添加正确,但是当我从 dom 中查询它时,它显示为 null。我之前对服务器进行了一次获取,但它有一个 await 来减慢它的速度。有什么想法吗?
这是我的 javascript 代码
socket.on('receive_message', async function(data) {
let messaged_user_li = document.getElementById('messaged_user_li'+data['from_user']);
console.log('messaged_user_li: '+messaged_user_li)
if (messaged_user_li == null) {
console.log('if fired')
await fetch('/get_messaged_user/'+data['from_user']).then((response) => {
response.json().then((data2) => {
loadMessagedUser(data2[0].id, data2[0].avatar, data2[0].username, data2[0].last_seen);
});
});
console.log(data['from_user'])
messaged_user_li = document.getElementById('messaged_user_li'+data['from_user']);
//despite this element being added this log shows nulls which causes issues later on in the code.
console.log('messaged_user_li: '+messaged_user_li)
}
let message_user = document.getElementById('message_user'+data['from_user']);
let message_target = document.getElementById("message_target"+data['from_user']);
if (messaged_user_li.classList.contains('active') == false) {
messaged_user_li.classList.add('flash-message');
}
if (message_user != null) {
data = `
<li class="clearfix">
<div class="message-data text-right">
<span class="message-data-time">just now</span>
</div>
<div class="message other-message float-right">`+data['message']+`</div>
</li>`;
message_target.innerHTML += data;
//Move scroller to bottom when message received
myDiv = message_user.querySelector(".chat-history");
myDiv.scrollTop = myDiv.scrollHeight;
}
});
您只需要等待此承诺完成:
await fetch('/get_messaged_user/'+data['from_user']).then((response) => {
// ...
});
回调中没有这个:
response.json().then((data2) => {
loadMessagedUser(data2[0].id, data2[0].avatar, data2[0].username, data2[0].last_seen);
});
这就是为什么您的代码继续执行并尝试获取添加的元素(尚未添加)的原因。
要解决此问题,您可以链接 then
调用:
await fetch('/get_messaged_user/'+data['from_user'])
.then((response) => response.json())
.then((data2) => loadMessagedUser(data2[0].id, data2[0].avatar, data2[0].username, data2[0].last_seen));
或再次使用await
:
const response = await fetch('/get_messaged_user/'+data['from_user']);
const data2 = await response.json();
loadMessagedUser(data2[0].id, data2[0].avatar, data2[0].username, data2[0].last_seen);
我有一个聊天程序,可以在收到消息时向 dom 添加一个元素。此元素添加正确,但是当我从 dom 中查询它时,它显示为 null。我之前对服务器进行了一次获取,但它有一个 await 来减慢它的速度。有什么想法吗?
这是我的 javascript 代码
socket.on('receive_message', async function(data) {
let messaged_user_li = document.getElementById('messaged_user_li'+data['from_user']);
console.log('messaged_user_li: '+messaged_user_li)
if (messaged_user_li == null) {
console.log('if fired')
await fetch('/get_messaged_user/'+data['from_user']).then((response) => {
response.json().then((data2) => {
loadMessagedUser(data2[0].id, data2[0].avatar, data2[0].username, data2[0].last_seen);
});
});
console.log(data['from_user'])
messaged_user_li = document.getElementById('messaged_user_li'+data['from_user']);
//despite this element being added this log shows nulls which causes issues later on in the code.
console.log('messaged_user_li: '+messaged_user_li)
}
let message_user = document.getElementById('message_user'+data['from_user']);
let message_target = document.getElementById("message_target"+data['from_user']);
if (messaged_user_li.classList.contains('active') == false) {
messaged_user_li.classList.add('flash-message');
}
if (message_user != null) {
data = `
<li class="clearfix">
<div class="message-data text-right">
<span class="message-data-time">just now</span>
</div>
<div class="message other-message float-right">`+data['message']+`</div>
</li>`;
message_target.innerHTML += data;
//Move scroller to bottom when message received
myDiv = message_user.querySelector(".chat-history");
myDiv.scrollTop = myDiv.scrollHeight;
}
});
您只需要等待此承诺完成:
await fetch('/get_messaged_user/'+data['from_user']).then((response) => {
// ...
});
回调中没有这个:
response.json().then((data2) => {
loadMessagedUser(data2[0].id, data2[0].avatar, data2[0].username, data2[0].last_seen);
});
这就是为什么您的代码继续执行并尝试获取添加的元素(尚未添加)的原因。
要解决此问题,您可以链接 then
调用:
await fetch('/get_messaged_user/'+data['from_user'])
.then((response) => response.json())
.then((data2) => loadMessagedUser(data2[0].id, data2[0].avatar, data2[0].username, data2[0].last_seen));
或再次使用await
:
const response = await fetch('/get_messaged_user/'+data['from_user']);
const data2 = await response.json();
loadMessagedUser(data2[0].id, data2[0].avatar, data2[0].username, data2[0].last_seen);