简体   繁体   English

我正在尝试从用户输入中获取一些数据到 JSON 文件中。我收到此消息

[英]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.

相关问题 我试图从html中的json获取数据。 我不想使用Ajax,因为 - I am trying to get the data from json in html. I don't want to use Ajax because 当我尝试从提示框中获取用户输入并在数组中显示这些输入值时,我收到重复的输入值作为 Output - I am getting repeated input values as an Output when I am trying to get User Input from Prompt box and showing those input values in an Array 在我的javascript函数中,我试图从表单用户输入中获取所创建的函数的值,并输出答案 - In my javascript function I am trying to get the values from a form user input to a function I created and the output the answer 我正在尝试从bitbns.com获取json数据,但出现错误 - I am trying to get json data from bitbns.com but getting an error 我正在尝试从可绘制对象中获取位图 - i am trying to get Bitmap from a drawable 我正在尝试从Rest API中获取json数据以进行反应,但是它显示了一些错误 - I am trying to fetch json data from Rest API into react, but it showing some error 尝试从服务器获取JSON数据时出现一些错误 - Some errors when I tried to get JSON data from a server 我正在尝试从 firestore 获取用户并将它们存储在 javascript 的 Map 但由于某种原因 map 是空的 - I am trying to get a users from firestore and store them in Map in javascript but for some reasone the map is empty 我正在尝试从JSON文件中检索数据,但是我无法从特定属性中检索信息 - I am trying to retrieve data from a JSON file, but I am having trouble retrieving information from a specific property 我想念什么? 试图让正则表达式工作来自json - What am I missing? Trying to get Regex to work that came from json
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM