[英]I am trying to get some data from user input into a JSON file.I get this message
I get this JSON error that tells me that I have a syntax error an I can't figure out where I made the mistake.我收到这个 JSON 错误,告诉我我有一个语法错误,我不知道我在哪里犯了错误。 I have provided the code in case if helps.
如果有帮助,我已经提供了代码。
var isNode= typeof module !=="undefined"
var clientData;
var auxClientData;
var aux=[];
var k=0;
if (!isNode)
{
var storedObjects=JSON.parse(localStorage.getItem("objects"));
console.log(storedObjects);
var ok=false;
buttonConfirm.addEventListener("click",function(){
for(var i=0;i<inputs.length;i++)
{
var inputShow=inputs[i].value;
if (inputShow!=null && inputShow!="")
{
aux[k++]=inputShow;
}
else
{
alert("ALL THE FIELDS MUST BE COMPLETED! ");
ok=true;
break;
}
}
clientData={fullName:aux[0],emailAddress:aux[1],phoneNumber:aux[2]};
//localStorage.setItem("clientData",JSON.stringify(clientData));
// console.log(clientData);
if (ok==true){
alert("THANK YOU FOR YOUR PURCHASE! CHECK YOUR E-MAIL")
}
console.log(JSON.parse(JSON.stringify(clientData)));
})
}
else
{
var clientData={"fullName":aux[0],"emailAddress":aux[1],"phoneNumber":aux[2]};
var fs=require("fs");
auxClientData=JSON.stringify(clientData);
fs.writeFile("clients.json",auxClientData,finished)
function finished()
{
console.log("ok");
var ok=fs.readFileSync("clients.json");
var test=JSON.parse(ok);
console.log(test);
}
}
Here is the error:这是错误:
SyntaxError: Unexpected end of JSON input at JSON.parse ()
SyntaxError: JSON.parse () 处的 JSON 输入意外结束
I've just realised what the problem is :我刚刚意识到问题是什么:
var clientData;
auxClientData=JSON.stringify(clientData);
clientData inside your else statement is never set and it will be undefined.您的 else 语句中的 clientData 永远不会设置,它将是未定义的。 so you will need to redefine it inside the else.
所以你需要在 else 中重新定义它。 aux will need to be available and in scope further up your program for this to work.
aux 将需要可用,并且在您的程序范围内进一步扩展才能使其正常工作。 You can confirm my assumption by manually setting clientData to something else inside the else statement.
您可以通过在 else 语句中手动将 clientData 设置为其他内容来确认我的假设。
else
{
var fs=require("fs");
clientData={"fullName":aux[0],"emailAddress":aux[1],"phoneNumber":aux[2]};
auxClientData=JSON.stringify(clientData);
fs.writeFile("clients.json",auxClientData,finished)
function finished()
{
console.log("ok");
}
var ok=fs.readFileSync("clients.json");
var test=JSON.parse(ok);
console.log(test);
}
------------------------------YOUR CODE. ------------------------------您的代码。
var isNode = typeof module !== "undefined"
var clientData;
var auxClientData;
var aux = [];
var k = 0;
if (!isNode) {
// INSIDE THE IF
var storedObjects = JSON.parse(localStorage.getItem("objects"));
console.log(storedObjects);
var ok = false;
//add a listener inside the if statement for the click.
buttonConfirm.addEventListener("click", function () {
// when it is clicked.
for (var i = 0; i < inputs.length; i++) {
var inputShow = inputs[i].value;
if (inputShow != null && inputShow != "") {
//POPULATE THE AUX ARRAY adding one to k each time?
// seems weird, why is this not aux[i] = inputShow;
aux[k++] = inputShow;
}
else {
alert("ALL THE FIELDS MUST BE COMPLETED! ");
ok = true;
break;
}
}
clientData = { fullName: aux[0], emailAddress: aux[1], phoneNumber: aux[2] };
//localStorage.setItem("clientData",JSON.stringify(clientData));
// console.log(clientData);
if (ok == true) {
alert("THANK YOU FOR YOUR PURCHASE! CHECK YOUR E-MAIL")
}
console.log(JSON.parse(JSON.stringify(clientData)));
})
}
else {
//inside the else.
//aux is an empty array here. so aux[0] = undefined, aux[1] = undefined etc.
//i.e. the button hasn't been pressed to populate it at this point.
var clientData = { "fullName": aux[0], "emailAddress": aux[1], "phoneNumber": aux[2] };
var fs = require("fs");
auxClientData = JSON.stringify(clientData);
fs.writeFile("clients.json", auxClientData, finished)
function finished() {
console.log("ok");
var ok = fs.readFileSync("clients.json");
var test = JSON.parse(ok);
console.log(test);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.