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

时间:2019-04-27 01:26:08

标签: node.js typescript

我正在尝试将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”。

请帮助和帮助。

3 个答案:

答案 0 :(得分: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循环内for,则每个pdObj都会创建for loop,因此,不同的引用对象会附加到{{ 1}}。


或者您可以在pdObjArray之前使用对象克隆
pdObjArray.push(pdObj);
What is the most efficient way to deep clone an object in JavaScript?

答案 1 :(得分:0)

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

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

请尝试在var函数内的第一个for循环中更改doJobsSeqCall,然后重试。

答案 2 :(得分:0)

由于files是一个字符串数组,因此您可以尝试执行foreach,然后执行其中的所有逻辑。

files.forEach(file => { //logic here })

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