简体   繁体   中英

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. 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 ()

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. so you will need to redefine it inside the else. aux will need to be available and in scope further up your program for this to work. You can confirm my assumption by manually setting clientData to something else inside the else statement.

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);

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM