I have two JSON files. I want to compare the JSON objects in both files; json1 and json2. If there is an object in the array in the first json file (json1) file that is not present in second json file (json2), I would like to pass it through the below Nightmare js code then push it the object to the second json file using .push().
JS CODE:
var Nightmare = require('nightmare');
var nightmare = Nightmare({
show: true
});
var json1 = require('./json1.json')
var json2 = require('./json2.json')
for (var i = 0; i < json1.length; i++) {
for (var c = 0; c < json2.length; c++) {
if (json1[i] !== (json2[c])) {
console.log(json1[i])
return nightmare
.goto(json1[0].searchOn)
.insert('.gLFyf', json1[0].searchText)
.wait(3000)
.end()
.evaluate((json2, json1) => {
return json2[c].push(json1[i])
}, json2, json1)
.then()
} else {
console.log('End!')
}
}
}
JSON1 Data
[
{
"searchOn": "https://www.google.com",
"searchText": "I love google"
},
{
"searchOn": "https://www.google.com",
"searchText": "I'm hungry, where can I eat?'"
}
]
JSON2 Data
[
{
"searchOn": "https://www.google.com",
"searchText": "What's the date?"
},
{
"searchOn": "https://www.google.com",
"searchText": "What is the internet"
},
{
"searchOn": "https://www.google.com",
"searchText": "What's the weather like today?"
}
]
However, the Js code gives me this error: UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'push' of undefined.
The code also does not execute the nightmare js code on all objects on the loop (only performs the task for the first item alone.
Please advice on how I can fix these errors.
Kind regards.