Node.js接口数组未正确填充

问题描述 投票:0回答:3

我试图将4个文件读入一个对象数组。但是,数组中的对象值都是最后一个对象。我想知道我为这种行为做了什么错。

代码(代码已经简化,因此可能没有多大意义):

import * as fs from "fs";

interface axObj {
    id: String;
    name: String;
    body: String;   
};
const logger = console;
const mypath = "/Users/autorun/Documents/Projects/Axway/Projects/axDeploy/data/rtczip/PD_only/";
const files:String[] = [
    "PD.DEVOPS.TEST.1.FS.json",
    "PD.DEVOPS.TEST.1.SFTP.json",
    "PD.DEVOPS.TEST.1.json",
    "PD.DEVOPS.TEST.1_AS2.json"
];

function doJobsSeqCall() {

    var pdObj: axObj = {
        id: "",
        name: "PD.Object",
        body: ""
    };
    var pdObjArray: axObj[] = [];

    var i = 0;
    var data: string = "";
    for (var file of files) {
        logger.debug("\nReading file: " + mypath + file);
        const data = fs.readFileSync(mypath + file, "utf8");
        if (!data) {
            throw "No data to post";
        }
        pdObj.id = String(i + 1);
        pdObj.body = data;
        pdObj.name = file;
        pdObjArray.push(pdObj);

        //Everything is correctly set here.
        logger.debug("Setting up axObj [" + pdObjArray[i].id + "]");
        logger.debug("Data file is valid with size: [" + pdObjArray[i].body.length + "]  file: [" + pdObjArray[i].name + "]");

        i++;
    }

    //Right after the for loop. This checking showed the last item was populated to all other array items which I don't understand at all.
    logger.debug("\n\nChecking pdObjArray Content");
    i = 1;
    for (let iAxObj of pdObjArray) {
        console.log("axSeqCall index: [" + i++ + "] name: [" + iAxObj.name + "]");
    }
};

try {
    doJobsSeqCall();
} catch (err) {
    logger.error(`Error: ${err}`);
    process.exit(-1);
};

结果在日志中:

Reading file: /Users/autorun/Documents/Projects/Axway/Projects/axDeploy/data/rtczip/PD_only/PD.DEVOPS.TEST.1.FS.json
Setting up axObj [1]
Data file is valid with size: [754]  file: [PD.DEVOPS.TEST.1.FS.json]

Reading file: /Users/autorun/Documents/Projects/Axway/Projects/axDeploy/data/rtczip/PD_only/PD.DEVOPS.TEST.1.SFTP.json
Setting up axObj [2]
Data file is valid with size: [1625]  file: [PD.DEVOPS.TEST.1.SFTP.json]

Reading file: /Users/autorun/Documents/Projects/Axway/Projects/axDeploy/data/rtczip/PD_only/PD.DEVOPS.TEST.1.json
Setting up axObj [3]
Data file is valid with size: [1507]  file: [PD.DEVOPS.TEST.1.json]

Reading file: /Users/autorun/Documents/Projects/Axway/Projects/axDeploy/data/rtczip/PD_only/PD.DEVOPS.TEST.1_AS2.json
Setting up axObj [4]
Data file is valid with size: [874]  file: [PD.DEVOPS.TEST.1_AS2.json]


Checking pdObjArray Content
axSeqCall index: [1] name: [PD.DEVOPS.TEST.1_AS2.json]
axSeqCall index: [2] name: [PD.DEVOPS.TEST.1_AS2.json]
axSeqCall index: [3] name: [PD.DEVOPS.TEST.1_AS2.json]
axSeqCall index: [4] name: [PD.DEVOPS.TEST.1_AS2.json]

问题:

我不希望“检查pdObjArrary内容”中的所有名称都是[PD.DEVOPS.TEST.1.AS2.json]。我期望所有4个不同的文件名,例如“PD.DEVOPS.TEST.1” .FS.json“,”PD.DEVOPS.TEST.1.SFTP.json“,”PD.DEVOPS.TEST.1.json“,”PD.DEVOPS.TEST.1_AS2.json“那里。

请帮助和协助。

node.js typescript
3个回答
0
投票

for (const file of files)for (let file of files)

如果必须访问异步调用中的var,则不能在for循环中使用file关键字。它不会捕获file的确切值。

请尝试在var函数内的第一个for循环中更改doJobsSeqCall并再次尝试。


1
投票
for (var file of files) {
    var pdObj: axObj = {
        id: "",
        name: "PD.Object",
        body: ""
    };

...
}

请在pdObj循环中声明变量for

为什么?: 如果pdObjfor循环之外,

pdObj = ...
pdObjArray.push(pdObj);

上面的代码表示:将相同的引用变量pdOjb附加到pdObjArray。 因此,pdObjArray包含相同的参考数组 这意味着只显示最后一个值。

但是,如果在pdObj循环内部的forpdObj是每个for loop创建的,那么,不同的参考对象被附加到pdObjArray


或者你可以在pdObjArray.push(pdObj);之前使用Object clone pdObjArray.push(JSON.parese(JSON.stringfy(pdObj))); What is the most efficient way to deep clone an object in JavaScript?


0
投票

由于files是一个字符串数组,您可以尝试执行foreach然后执行其中的所有逻辑。 files.forEach(file => { //logic here })

看看Mozilla网站https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach上的例子

© www.soinside.com 2019 - 2024. All rights reserved.