如何清除JS控制台中未定义/未定义的消息



我让我的Javascript与我的API网关对话,我的计数器一直在上升,但在我的控制台上它说";"未定义";,我不知道如何解决这个问题。我不确定我的代码中发生了什么变化,但我收到了一条不同的消息,说的不是通常的";未定义";。

fetch('https://wwrr7r0kj5.execute-api.eu-west-2.amazonaws.com/dev/')
.then(response => response.text())
.then(contents =>{
console.log(contents);
document.getElementById("visitors").innerHTML = contents
})
Promise {<pending>}
VM126:4
296

<script>
fetch('aws api')
.then(response => response.text())
.then(contents => console.log(contents))
document.getElementById("visitors").innerhtml = content
</script>

<div>
<p  style="text-align: center;color: white;"> 
Visitors: <span id="visitors">0</span> 
</p>
</div>

此处:

<script>
fetch('aws api')
.then(response => response.text())
.then(contents => console.log(contents))
document.getElementById("visitors").innerHTML = content
</script>

content没有定义,你可能是指contents,但你必须在承诺得到解决后才能定义:

<script>
fetch('aws api')
.then(response => response.text())
.then(contents =>{
console.log(contents);
document.getElementById("visitors").innerhtml = contents
})
</script>

最新更新