尝试访问javascript对象的属性时属性未定义

时间:2020-10-11 15:34:03

标签: javascript object properties undefined

我对此问题不知所措,无法解决。

我正在解析5行示例csv(使用csv-parser nodejs模块),所有4个对象的所有第一个属性都未定义。

我在这里打印对象(obj实例)

{ 'separator': 'test',
cor: 'Esmeralda',
  tam: [ '40', '40' ],
  brand: 'test2',
  ref: '20441',
  price: '149.00',
  discount: '',
  price_final: '149.00',
  other: '42 e 46 Safira, 38 e 44 agua marina, 36 Quartzo Rosa',
  description: '',
  photo: [ '7894', '7898', '7900', '7823' ] }

在obj上执行JSON.stringify时,结果是:

{"separator":"test","cor":"Esmeralda","tam":["40","40"],"brand":"test2","ref":"20441","price":"149.0
0","discount":"","price_final":"149.00","other":"42 e 46 Safira, 38 e 44 agua marina, 36 Quartzo Rosa","description":"","photo":["7894","7898","7900","7823"]}

当我做obj ['separator']或obj.separator时,我不确定,到底是什么?

有任何线索吗?

编辑:

这是CSV

separator;cor;tam;brand;ref;price;discount;price_final;other;description;photo
test;Esmeralda;40 e 40;test2;20441;149,00;;149,00 €;42 e 46 Safira, 38 e 44 agua marina, 36 Quartzo Rosa;;7894, 7898, 7900, 7823
test;Safira ;42 e 46;test2;20441;149,00;;149,00 €;40 Esmeralda, 38 e 44 Água Marinha, 36 Quartzo Rosa;;7908, 7910, 7823
test;Quartzo Rosa;36;test2;20441;149,00;;149,00 €;40 Esmeralda, 42 e 46 Safira, 38 e 44 Água Marinha;;7913, 7901, 7902, 7823,
test;Água Marinha;38 e 44;test2;20441;149,00;;149,00 €;40 Esmeralda, 42 e 46 Safira, 36 Quartzo Rosa;;7907, 7905, 7823,

这是我阅读的地方:

const csv = require('csv-parser')
const fs = require('fs')
const results = [];
const resultsByRef = {};

fs.createReadStream('vestidos_sample.csv')
  .pipe(csv({ separator: ';' }))
  .on('data', (data) => results.push(data))
  .on('end', () => {
      uniformData();


      
  });

function uniformData(){
    for(let line of results){
        console.log(line['separator']);   // >> undefined        
    }
}

1 个答案:

答案 0 :(得分:0)

使用以下csv选项进行管理:

 fs.createReadStream('vestidos_sample.csv')
  .pipe(csv({ skipLines:1,separator: ';', headers: ['separator','cor','tam','brand','ref','price','discount','price_final','other','description','photo'] }))
  .on('data', (data) => results.push(data))
  .on('end', () => {
      uniformData();

  });
相关问题