I am trying to parse xml file to json and then bulk index it into elastic search. This is my code:
require('array.prototype.flatmap').shim()
const fs = require('fs');
const { Client } = require('@elastic/elasticsearch')
const client = new Client({
node: 'http://localhost:9200'
})
const xml2js = require('xml2js');
const parser = new xml2js.Parser();
fs.readFile(__dirname + '/data.xml', (err, data) => {
parser.parseString(data, (err, result) => {
const jsonString = JSON.stringify(result);
var jsonObj = JSON.parse(jsonString);
run(jsonObj).catch(console.log)
});
});
async function run (dataset) {
await client.indices.create({
index: 'entity'
}, { ignore: [400] })
console.log(dataset)
const body = dataset.flatMap(doc => [{ index: { _index: 'entity' } }, doc])
// code removed for brevity
}
I can console.log(dataset) and see its data.
But when I run flatMap, it hits error:
TypeError: dataset.flatMap is not a function
But if I pass in hardcoded value for dataset like below, it works.
const dataset = [{
id: 1,
text: 'If I fall, don\'t bring me back.',
user: 'jon1',
date: new Date()
}, {
id: 2,
text: 'Winter is coming',
user: 'ned',
text: 'A Lannister always pays his debts.',
user: 'tyrion',
date: new Date()
}, {
id: 4,
text: 'I am the blood of the dragon.',
user: 'daenerys',
date: new Date()
}, {
id: 5, // change this value to a string to see the bulk response with errors
text: 'A girl is Arya Stark of Winterfell. And I\'m going home.',
user: 'arya',
date: new Date()
}]
Here is my package.json
{
"name": "elasticsearch",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@elastic/elasticsearch": "^7.11.0",
"array.prototype.flatmap": "^1.2.4",
"express": "^4.17.1",
"fs": "0.0.1-security",
"util": "^0.12.3",
"xml2js": "^0.4.23"
},
"devDependencies": {
"eslint": "^7.19.0"
}
}
from TypeError: xxx.flatMap is not a function

No comments:
Post a Comment