获取"TypeError: invalid 'in'操作数a"在WordPress

Getting a "TypeError: invalid 'in' operand a" within WordPress

本文关键字:quot WordPress 操作数 invalid TypeError 获取 in      更新时间:2023-09-26

所以我得到一个TypeError:无效的'in'操作和WordPress内的错误(不确定这是否与之相关),我不确定代码有什么问题。

下面是问题代码:

e_jsonObj = [];            
$.each(the_wpcc_posts, function(i, the_wpcc_post) {
    var e_post_id = the_wpcc_post[i].ID;
    var e_title = the_wpcc_post[i].post_title;
    var e_start = the_wpcc_post[i].post_date_gmt;
    e_item = {}
    e_item ["id"] = e_post_id;
    e_item ["title"] = e_title;
    e_item ["start"] = e_start;
    console.log(e_item);
    e_jsonObj.push(e_item);         
});

尝试在each循环之前解析数据。似乎你正在尝试迭代一个字符串,这导致了这个错误。

e_jsonObj = [];
data = $.parseJSON(the_wpcc_posts);  // parsing data          
$.each(data, function(i, the_wpcc_post) {
    var e_post_id = the_wpcc_post[i].ID;
    var e_title = the_wpcc_post[i].post_title;
    var e_start = the_wpcc_post[i].post_date_gmt;
    e_item = {}
    e_item ["id"] = e_post_id;
    e_item ["title"] = e_title;
    e_item ["start"] = e_start;
    console.log(e_item);
    e_jsonObj.push(e_item);         
});