JS - TypeError:值不可迭代

JS - TypeError: value is not iterable

提问人:Damiano 提问时间:9/5/2023 更新时间:9/5/2023 访问量:93

问:

我是JS的新手,我需要从支持JS的HMI面板(Weintek)执行API POST请求。 我需要为机器的数据创建一个具有定义结构的对象。

This is how I created the Object (objWebApi) that contains other variables and other Objects

我尝试构建代码以以虚拟方式获取我需要发送的字符串。

最初,我创建了一个字典来包含我的最终数据,以便使用 stringify 发送。 然后我迭代第一个“层”中的变量(对不起我的英语),所以名称、id、描述等。

// initialize final object to send
var objWebApi_Final = {

};

// Subscription - Read and Elaboration of data
this.config.Sub_GetData.onResponse(async (Sub_GetData_Err, Sub_GetData_Data) => {

    if (Sub_GetData_Err) {
        logger.log("Sub_GetData - ERROR: ", err.message);
    } else {
        let command = Sub_GetData_Data.values[0];
        // Do the rest if the value of Subscription goes OFF -> ON
        if (command === 1) {

            // Counting the number of object declared for this.config.objWebApi
            const objectCount = Object.keys(this.config.objWebApi).length;

            // Declaring a var for counting for iterations
            var i = 0;
            for (const [key, value] of Object.entries(this.config.objWebApi)) {
                objWebApi_Final, i = await elaborate(objWebApi_Final, this.config, key, value, true, i, objectCount);
            };
        };
    };
});

我称该函数为detail:

async function elaborate(dict, config, Key, Value, isPrimitive, i, objectCount) {
    var type = resolveType(Value);
    if (type === "Object") {
        var tempDict = {};
        var nameObj = (Key).toString();
        const objectCount_internal = Object.keys(Value).length;

        for await (const [key, value] of Object.entries(Value)) {
            tempDict, i = await Promise.all(elaborate(tempDict, config, key, value, false, i, objectCount_internal));
        dict[nameObj] = tempDict;
        };
    } else {
        if (type === "Subscription") {
            if (isPrimitive === true) {
                dict, i = getData_String(config, Key, Value, dict, i, objectCount);
            } else {
                dict = getData_SubElement_String(Key, Value, dict);
            };
        } else {
            if (Value.dataType === 0){
                if (isPrimitive === true) {
                    dict, i = getData_Bit(config, Key, Value, dict, i, objectCount);
                } else {
                    dict = getData_SubElement_Bit(Key, Value, dict);
                };
            } else {
                if (isPrimitive === true) {
                    dict, i = getData_Word(config, Key, Value, dict, i, objectCount);
                } else {
                    dict = getData_SubElement_Word(Key, Value, dict);
                };
            };
        };
    };

    if (isPrimitive === true && type === "Object") {
        i = i + 1;
        // Call the function for sending data
        await Promise.all(callRequest(dict, objectCount, i));
    };
    return dict, i;
};

我在这里粘贴了一个 GetData 函数(都非常相似)。

async function getData_String(config, key, value, dict, i, objectCount) {
    var length = value.length;
    var address = value.address;

    var utf8decoder = new TextDecoder();
    await driver.promises.getData(address, length).then((valoreAcquisito) => {
        var bufferAsUint8Array = new Uint8Array(valoreAcquisito.buffer);
        dict[key] = utf8decoder.decode(bufferAsUint8Array);
        dict[key] = dict[key].replace('\0', '').replace(/\0/g, '');
        i = i + 1;
        // Call the function for sending data
        callRequest(config, objectCount, i);
    });
    return dict, i;
};

我的问题是:当我遇到一个对象时,比如我示例中的“SOTTO”,我再次调用函数“elaborate”,将键和值作为参数。 我收到一个错误:TypeError:value 在 elaborate (:103) 根本不可迭代(本机)

我开始阅读生成器/迭代器、地图等东西,但我无法解决这个错误(也是因为我对迭代器和其他东西了解不多......

先谢谢你。

JavaScript TypeError 可迭代

评论


答: 暂无答案