javascript - 未捕获( promise )TypeError : Cannot set property 'textContent' of null

标签 javascript api

the javascript file picture

这个文件,上面写着

Uncaught (in promise) TypeError: Cannot set property 'textContent' of null
    at app.js:25
window.addEventListener('load', () => {
    let long;
    let lat;
    let temperatureTimeZone = document.querySelector("temperature-timezone");
    let temperatureDegree = document.querySelector("temperature-degree");
    let temperatureDescription = document.querySelector("temperature-description");

    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(position => {
            long = position.coords.longitude;
            lat = position.coords.longitude;

            const proxy = `https://cors-anywhere.herokuapp.com/`;
            const api = `${proxy}https://api.darksky.net/forecast/1299787a9e937056e2ee6cb21ebfc2d4/${lat},${long}`;

            fetch(api)
                .then(response => {
                    return response.json();
                })
                .then(data => {
                    console.log(data);
                    const { temperature, summary, precipType, icon } = data.currently;

                    // set Dom Elements from APi
                    temperatureDegree.textContent = temperature;
                });
        });
    } else {
        h1.textcontent = "Hey reload site and allow geolocation";
    }
});

最佳答案

什么是“温度”?如果它是一个 id,那么你必须做“#temperature-degree”。如果它是一个类,那么你必须做“.temperature-degree”。如果它是一个标签,那么你没有任何符号,但“温度-度数”不应该是一个标签。如果您仍然感到困惑,请查看它的文档:developer.mozilla.org/docs/Web/API/Document/querySelector

关于javascript - 未捕获( promise )TypeError : Cannot set property 'textContent' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56241224/

相关文章:

javascript - 我的 redux-saga 有什么问题

当选择其他元素时,javascript切换类关闭

java - 如何从 JSF 中的托管 Bean 检索数组列表

java - Nashorn 脚本引擎在 Java 8 SE 嵌入式上为空

php - 如何设置json格式的请求体

rest - 如何使用 RestSharp 保存不记名 token 以备将来使用

javascript - 从alertify.dialog实现中引用外部html文件

python-3.x - Marvel 开发者 API 调用,仅收到一页

python - Flask-restful API 不接受 json

javascript - speechSynthesis API 示例给出错误