Node.js没有更新变量

发布于 2025-02-05 20:44:51 字数 819 浏览 2 评论 0原文

我正在尝试编写一个功能,我正在尝试读取一个CSV文件,在此之前,我试图检查该Perticular CSV文件的标题是否

 let validHeaders = false;
 fs.createReadStream(path)
 .pipe(csv.parse({headers : true , ignoreEmpty : true}))
 .on('headers',async function(headers){
  validHeaders = await CustomValidation.isValidRowsExists(headers);
  console.log(validHeaders); // here i am getting true
 .on('data',async function(req,res,data){
  console.log(validHeaders); //here i am getting false
  if(validHeaders){ //do something

class CustomValidation{
 isValidRowsExists = async(csvRows) =>{
  for(let i=0;i<csvRows.length;i++){
    if(csvRows[0]==='Col1 && csvRows[1]==='Col2 ....){
    return true;
   }else{ 
    return false;
   }
  }
 }
}
module.exports = new CustomValidation;

正确 使用有效的黑头值

I am trying to write one functionality where i am trying to read one csv file , and before that i am trying to check the headers of that perticular csv file are given correctly or not.Below is my code

 let validHeaders = false;
 fs.createReadStream(path)
 .pipe(csv.parse({headers : true , ignoreEmpty : true}))
 .on('headers',async function(headers){
  validHeaders = await CustomValidation.isValidRowsExists(headers);
  console.log(validHeaders); // here i am getting true
 .on('data',async function(req,res,data){
  console.log(validHeaders); //here i am getting false
  if(validHeaders){ //do something

This is my CustomValidation class

class CustomValidation{
 isValidRowsExists = async(csvRows) =>{
  for(let i=0;i<csvRows.length;i++){
    if(csvRows[0]==='Col1 && csvRows[1]==='Col2 ....){
    return true;
   }else{ 
    return false;
   }
  }
 }
}
module.exports = new CustomValidation;

How i can use the validHeaders value

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁绊已千年 2025-02-12 20:44:51

可能您需要这样的东西:

const main = async () => {
  const csvStream = fs
    .createReadStream(path)
    .pipe(csv.parse({ headers: true, ignoreEmpty: true }));

  const validHeaders = await new Promise((resolve) =>
    csvStream.on("headers", (headers) =>
      resolve(CustomValidation.isValidRowsExists(headers))
    )
  );

  csvStream.on("data", (data) => {
    if (validHeaders) {
      console.log(data);
    }
  });
};

main();

upd:
Heiko的演示和异步验证

Probably you need something like this:

const main = async () => {
  const csvStream = fs
    .createReadStream(path)
    .pipe(csv.parse({ headers: true, ignoreEmpty: true }));

  const validHeaders = await new Promise((resolve) =>
    csvStream.on("headers", (headers) =>
      resolve(CustomValidation.isValidRowsExists(headers))
    )
  );

  csvStream.on("data", (data) => {
    if (validHeaders) {
      console.log(data);
    }
  });
};

main();

UPD:
A demo for Heiko with async validation
long validation demo

风苍溪 2025-02-12 20:44:51

如果 customValidation.isvalidrowsexists是异步的(但请参见下文):

Moondef的解决方案附加.on(“数据”) 等待customValidation.isvalidrowsexists的结果。但是到那时,数据事件可能已经发生。

我建议在确定标题有效性后,同步附加处理程序,并使用承诺将数据异步登录。

var validHeaders;
csvStream.on("headers", function(headers) {
  validHeaders = Promise.resolve(CustomValidation.isValidRowsExists(headers));
})
.on("data", function(data) {
  validHeaders.then(function(valid) {
    if (valid) console.log(data);
  });
});

但是,customValidation.isvalidrowsexists如图所示,实际上是不是异步,因为它包含不包含等待,并且不包含返回诺言。

If CustomValidation.isValidRowsExists were asynchronous (but see below):

moondef's solution attaches the .on("data") handler only after awaiting result of CustomValidation.isValidRowsExists. But by then, the data events may already have happened.

I suggest to attach the handlers synchronously, and use a promise to log the data asynchronously, after the header validity has been determined.

var validHeaders;
csvStream.on("headers", function(headers) {
  validHeaders = Promise.resolve(CustomValidation.isValidRowsExists(headers));
})
.on("data", function(data) {
  validHeaders.then(function(valid) {
    if (valid) console.log(data);
  });
});

However, the CustomValidation.isValidRowsExists as shown is in reality not asynchronous, because it contains no await and does not return a promise.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文