简体   繁体   English

如何在这个jQuery .each()循环中解析“undefined”?

[英]How do I resolve “undefined” in this jQuery .each() loop?

I'm working with the Shopatron API and most of the following code was adopted from their example(s). 我正在使用Shopatron API,他们的示例中采用了以下大部分代码。 It should be noted I'm using their API Library to make the call and it's connecting and working from that perspective. 应该注意的是,我正在使用他们的API库来进行调用,并且它正在从这个角度进行连接和工作。 I'm getting everything to output as I need it with the exception of additional product images which won't output because of an "undefined" error happening when I attempt to loop through .each() of the images in the array. 我正在将所有内容输出,因为我需要它,但由于当我尝试循环遍历数组中图像的.each()时发生“未定义”错误而无法输出的其他产品图像。

For reference, here is a screenshot of the console.log(data); 作为参考,这是console.log(data);的屏幕截图console.log(data); I'm "pulling" on for a product in Shopatron. 我正在为Shopatron的产品“拉”。

在此输入图像描述

I added some red arrows to point out what I'm interested in here. 我添加了一些红色箭头来指出我在这里感兴趣的东西。 Mainly I want to loop through that images array so I can output the additional images. 主要是我想循环遍历该图像数组,以便我可以输出其他图像。 Below you can see where I'm attempting to get the URL in my .each(); 下面你可以看到我在我的.each();尝试获取URL的.each(); loop and it's giving me undefined instead. 循环,它给了我undefined而不是。

Now here is a console.log(this.url); 现在这里是一个console.log(this.url); from inside my function and .each(); 来自我的函数和.each(); loop, so it's isolating out to just that images array. 循环,所以它只是隔离到那个图像数组。

在此输入图像描述

As you can see, I'm getting the URL, that's good. 如您所见,我正在获取URL,这很好。 But I'm also getting undefined which then outputs as such when I'm trying to use it to append an img tag. 但是当我尝试使用它来附加一个img标签时,我也会得到undefined输出。

Now I'll show you the code I'm using to do all of this. 现在我将向您展示我用来完成所有这些的代码。 There's a lot of other functions that are calling on product options and price and junk you can see in that first screen shot. 还有很多其他功能可以调用产品选项以及第一次屏幕截图中可以看到的价格和垃圾。 All of that is working. 所有这些都有效。 I'm only have trouble with the images so I'm isolating it down to that function. 我只是对图像有困难,所以我把它分离到那个功能。

var partNumber = '<?php echo $productID; ?>';
        $(document).ready(function() {
            Shopatron.getProduct({
                partNumber: partNumber
            },{
                success: function(p) {
                    outputProductName(p);
                    outputProductImage(p); // Here's the function up here
                    outputProductPrice(p);
                    outputDescription(p);
                    outputSpecs(p);
                },
                templateFriendly : false                                
            }
        );



        function outputProductImage(data) {

        var target = '#shopatron_product_image';
        var clickFunction = 
        $('#shopatron_product_image').html('<img src="' + data.image + '">');

        // Here's my loop that is giving me undefined...                                                        
        $(data.images).each(function() {

        $('#shopatron_additional_images').append("<li><a href='" + this.url + "' onclick=\"swapImage('#shopatron_product_image', '" + this.url + "'); return false;\"><img src='" + this.url + "'></a></li>");

        });

        return;
        }

Here's a console screenshot of what I'm getting for output for kicks. 这是一个控制台屏幕截图,显示了我为踢球输出的内容。

在此输入图像描述

Thank you for your suggestions and help with this. 感谢您的建议和帮助。

I believe this is because data.images is not a dom element and you are trying to create a jQuery object from it. 我相信这是因为data.images不是一个dom元素,你试图从它创建一个jQuery对象。

Instead of: 代替:

$(data.images).each(function() {

    $('#shopatron_additional_images').append("<li><a href='" + this.url + "' onclick=\"swapImage('#shopatron_product_image', '" + this.url + "'); return false;\"><img src='" + this.url + "'></a></li>");

});

try: 尝试:

$.each(data.images, function(i, image){
   $('#shopatron_additional_images').append("<li><a href='" + image.url + "' onclick=\"swapImage('#shopatron_product_image', '" + image.url + "'); return false;\"><img src='" + image.url + "'></a></li>");
})

This is jQuery's generic iterator for arrays and objects. 这是jQuery的数组和对象的通用迭代器。

I really want to thank everyone who made an effort to help me resolve this. 我真的要感谢所有努力帮助我解决这个问题的人。 It turned out to be an error on Shopatron's side of things. 事实证明,这是Shopatron方面的一个错误。 It was of course not generating a visible error, seemed more like their database was not updating. 它当然不会产生可见错误,似乎更像是他们的数据库没有更新。 Their suggestion was to simply re-add the pictures to the product items using the standard admin dashboard, nothing fancy. 他们的建议是使用标准管理仪表板简单地将图片重新添加到产品项目中,没什么特别的。 It fixed the issue. 它解决了这个问题。 Not exactly an advanced solution but maybe if someone else runs into this, it will help. 不完全是一个先进的解决方案,但也许如果其他人碰到这个,它会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用jQuery .each循环删除特定元素? - How do I remove specific elements using an jQuery .each loop? 在jQuery中,当你在每个()的范围内时,如何解决“this”的范围? - In jQuery, how do you resolve the scope of “this”, when you are in the scope of each()? 在带有JSON对象的jQuery中使用.each时,如何避免未定义的属性? - When using .each in jQuery with a JSON object how do I avoid undefined properties? 如何使用JQuery嵌套for-each循环以获取特定的li元素? - How do I do a nested for-each loop with JQuery to get specific li elements? 我怎样才能推迟每个循环的jQuery - How can I defer a jQuery each loop 类型错误:无法读取未定义的属性“then”---我该如何解决这个问题? - TypeError: Cannot read property 'then' of undefined --- How do I resolve this? 如何解决“无法读取未定义的属性(读取'ownerDocument')” - How do i resolve "Cannot read properties of undefined (reading 'ownerDocument')" 如何解决“无法调用未定义的方法&#39;流&#39;” - How do I resolve “Cannot call method 'stream' of undefined” 如何解决此“未定义函数” JavaScript错误? - How do I resolve this “Undefined function” JavaScript Error? IE9中每个循环在jQuery中的“未定义” - 'undefined' in jquery each loop in IE9
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM