【问题标题】:Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'. JAVASCRIPT未捕获的类型错误:无法在“节点”上执行“appendChild”:参数 1 不是“节点”类型。 JAVASCRIPT
【发布时间】:2018-12-04 14:35:52
【问题描述】:

这个想法是,无论搜索什么都会从单独的 API 中带回一些文本和图像。文字完全正常,但图像没有出现。

完整的消息错误是:

未捕获的类型错误:无法在“节点”上执行“appendChild”:参数 1 不是“节点”类型。在 Jdata.records.forEach.item 和 Array.forEach

 request.addEventListener("load", function() {


            if (request.status == 200) {
                var Jdata = request.response;
                Jdata.records.forEach(item => {

                    var results = document.getElementById("results");
                    var title = item.fields.title;
                    console.log(item.fields.title);

                    if (item.fields.hasOwnProperty('p_tag')) {
                        var fname = item.p_tag.substring(0,6);
                        var fid = item.fields.p_tag;
                        var image = `https://media.vam.ac.uk/media/thira/collection_images/${fname}/${fid}.jpg`;

                        var img2 = document.createElement('img');
                        img2.src = image;



                    }

                    else{

                    }

                    let h2 = document.createElement('h2');
                    h2.textContent = title;

                    results.appendChild(h2);
                    results.appendChild(img2);



                })



        } else {
            console.log("error: "+xhr.status);
        }

    });
    request.send();

【问题讨论】:

  • 你有一个空的else 分支。当代码采用该路径时,img2 未设置且为 undefined
  • @JaredSmith 不幸的是,即使在添加到 else 之后,未捕获的 TypeError 仍然保持不变
  • 使用更新后的代码编辑您的问题。
  • @JaredSmith 我已经在答案中写了代码
  • 再一次,请将代码编辑到您的问题中,除非您确实回答了自己的问题(这是完全允许的,但这里似乎并非如此)。

标签: javascript api foreach


【解决方案1】:

函数动作(){

    var results = document.getElementById("results");
    results.textContent = "";
    console.log(results);
    var sbar = document.getElementById("searchi").value;
    console.log(sbar);


    var request = new XMLHttpRequest;
    var url = 'https://www.vam.ac.uk/api/json/museumobject/search?q='+sbar;
    request.open('GET', url, true);
    request.responseType ='json';

    console.log("button was clicked");


    request.addEventListener("load", function() {


        if (request.status == 200) {
            var Jdata = request.response;
            Jdata.records.forEach(item => {

                var results = document.getElementById("results");
                var title = item.fields.title;
                console.log(item.fields.title);

                if (item.fields.hasOwnProperty('p_tag')) {
                    var fname = item.p_tag.substring(0,6);
                    var fid = item.fields.p_tag;
                    var image = `https://media.vam.ac.uk/media/thira/collection_images/${fname}/${fid}.jpg`;

                    var img2 = document.createElement('img');
                    img2.src = image;



                }

                else{
                    console.log("error");


                }

                let h2 = document.createElement('h2');
                h2.textContent = title;

                results.appendChild(h2);
                results.appendChild(img2);



            })



        } else {
            console.log("error: "+xhr.status);
        }

    });
    request.send();
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-20
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    • 1970-01-01
    • 1970-01-01
    • 2021-02-19
    相关资源
    最近更新 更多