我已经看到了这些类型的问题,并尝试了解决方案,但没有奏效。
我从UI向控制器发送了一个数组,在那里我有Node.js中faker.js的引用
我在控制器中的代码:
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在Faker.js中工作的预期代码是
const User = { FirstName: faker.name.firstName() } // Hard code and run this it is working fine如何接近这一点。
发布于 2017-11-28 17:14:51
JSON.stringify add extra“围绕所有键,您不能使用Usercheck.replace(/'"+/g,'')删除它们,否则您无法对其进行解析:
var a = JSON.stringify({e:5})
console.log(a) // {"e":5}
JSON.parse(a); // ok
JSON.parse("{e:5}"); // nokhttps://stackoverflow.com/questions/47525959
复制相似问题