이러한 유형의 질문을보고 해결책을 시도했지만 작동하지 않았습니다.JSON.Parse Faker.js에서 문자열을 JSOn으로 구문 분석하는 동안 위치 0의 오류 표시
내가 컨트롤러에 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
어떻게 접근합니까?
'faker.name.firstName'은 함수이므로 평가해야합니다. –
@ GrégoryBourgin 예. 그 작은 따옴표 (')가 없으면 작동합니다. 배열에는 성 등 여러 필드가있을 수 있습니다. –