JSON.Parse Showing error at position 0 while parsing string to JSOn in Faker.js

Viewed 174

I have seen these type of questions and tried the solutions but was not working.

I am sending an array from UI to the controller, there I have the reference for faker.js in Node.js

My code in controller:

var FirstName = req.body;                    // req.body has array
console.log(FirstName);                      // **Prints** { FirstName: 'faker.name.firstName()' }
const User = FirstName;                     // Didnt work because faker.name.firstName is as string
const Usercheck = JSON.stringify(GettingData[0]);
var response = Usercheck.replace(/['"]+/g,'')
console.log(response);                      // Here it removed the quotations but took total as string. "{ FirstName: faker.name.firstName()}"
JSON.parse(response);     // Tried to parse string as JSON but this shows the error at position 0

The expected code for working in Faker.js is

const User = { FirstName: faker.name.firstName() } // Hard code and run this it is working fine

How to approach this.

1 Answers
Related