为什么我总是从Chrome中得到“未被捕获的SyntaxError:意外的令牌u”?

时间:2022-09-01 04:50:14

I have spent a whole day on this, googling and searching for answers but still could not figure out.

我花了一整天的时间在上面搜索和搜索答案,但仍然无法找到答案。

My code is a bit long and it works well in Firefox but gets "Uncaught SyntaxError: Unexpected token u " from Chrome.

我的代码有点长,它在Firefox中运行良好,但是在Chrome中得到了“未被捕获的SyntaxError:意外的令牌u”。

Can anyone points me out where I am wrong? Thanks in advance!

有人能指出我哪里不对吗?提前谢谢!

// when the page loads, list all the current contacts
$(document).ready(function(){

    // check if localStorage database exists
    if(!localStorage.getItem("customerDatabase")){

        // define a JSON object to hold all current address
        var contacts = {
            "users":[
                {
                    "id":"1",
                    "name":"dennis",
                    "email":"dennisboys@gmail.com"
                },
                {
                    "id":"2",
                    "name":"zoe",
                    "email":"zoeisfemale@gmail.com"             
                }
            ]   
        } // end of contacts JSON object

        // stringify the object 
        var stringObject = JSON.stringify(contacts);                

        // store it into localStorage database
        var storedDatabase = localStorage.setItem("customerDatabase", stringObject);                                            

    } else {
        // list all customers upon page loads
        listJSONCustomers();        
    }   

    // list all current contacts from JSON object in localStorage
    function listJSONCustomers(){

      var displayHTML = "";
      var i;

      // get the data from localStorage
      var storedDatabase = localStorage.getItem("customerDatabase");

      // parse the data from string to JSON object
      var parseObject = JSON.parse(storedDatabase); 

      // access the users key of the JSON object
      var userObject = parseObject.users;

      // get the length of the object (how many customers the database has)
      var contactsLength = userObject.length;     

      for(i=0; i<contactsLength; i++){
          var trElement = '<tr id="address' + (i+1) + '">';
          var tdId = '<td id="id' + (i+1) + '">' + userObject[i].id + '</td>';
          var tdName = '<td id="name' + (i+1) + '">' + userObject[i].name + '</td>';
          var tdEmail = '<td id="email' + (i+1) + '">' + userObject[i].email + '</td>';
          var tdButton = '<td id="button"><button id="editButton' + userObject[i].id + '">Edit</button> | <button id="deleteButton' + userObject[i].id + '">Delete</button></td>';

          displayHTML += trElement + tdId + tdName + tdEmail + tdButton + '</tr>';
      }     

      $('#address_list').html(displayHTML);           
    }       

    // add customer to database  
    $('#saveCustomer').click(function(){

       if( $('#customerName').val() !== "" && $('#customerEmail').val() !== "" ){

           var customerName = $('#customerName').val();
           var customerEmail = $('#customerEmail').val();

           // get the data from localStorage
           var storedDatabase = localStorage.getItem("customerDatabase");

           // parse the data from string to JSON object
           var parseObject = JSON.parse(storedDatabase);    

           // access the users key of the JSON object
           var userObject = parseObject.users;     

           // get the new entry
           var newCustomerObject = {
                                  "id": userObject.length + 1,
                                  "name": customerName,
                                  "email": customerEmail
                                  };

           // push the new entry into the object                                                            
           userObject.push(newCustomerObject);

           // convert the object into string for localStorage
           var stringObject = JSON.stringify(parseObject);         

           // store the JSON object into localStorage
           var storedDatabase = localStorage.setItem("customerDatabase", stringObject);

           // list all customes again every time a database receives a new entry
           listJSONCustomers();     

       } else {
          alert("Please enter customer's name and email.");  
       }

    }); // end of $('#saveCustomer').click();


});

1 个解决方案

#1


18  

At some point, something you did corrupted the value of your LocalStorage for that key. LocalStorage can only store strings, so if you pass anything else to it, it will convert it to a string. Since your value is 'undefined', that means that at some point, you probably did something like this on accident:

在某个时候,您确实损坏了本地存储的值。LocalStorage只能存储字符串,所以如果您将其他字符串传递给它,它将把它转换为字符串。因为你的价值是“未定义的”,这意味着在某一时刻,你可能在事故中做过类似的事情:

var value;
localStorage.setItem('key', value);

In this case, value is undefined, which is not a string. When this gets saved, it will be converted however. Unfortunately, "undefined" is not valid JSON. That means that when it tries to parse, it will throw an exception.

在这种情况下,值是未定义的,它不是字符串。当它被保存时,它将被转换。不幸的是,“未定义”不是有效的JSON。这意味着当它试图解析时,会抛出异常。

To fix your issue, you should clear the bad value out with removeItem.

为了解决你的问题,你应该用removeItem清除坏值。

localStorage.removeItem("customerDatabase");

#1


18  

At some point, something you did corrupted the value of your LocalStorage for that key. LocalStorage can only store strings, so if you pass anything else to it, it will convert it to a string. Since your value is 'undefined', that means that at some point, you probably did something like this on accident:

在某个时候,您确实损坏了本地存储的值。LocalStorage只能存储字符串,所以如果您将其他字符串传递给它,它将把它转换为字符串。因为你的价值是“未定义的”,这意味着在某一时刻,你可能在事故中做过类似的事情:

var value;
localStorage.setItem('key', value);

In this case, value is undefined, which is not a string. When this gets saved, it will be converted however. Unfortunately, "undefined" is not valid JSON. That means that when it tries to parse, it will throw an exception.

在这种情况下,值是未定义的,它不是字符串。当它被保存时,它将被转换。不幸的是,“未定义”不是有效的JSON。这意味着当它试图解析时,会抛出异常。

To fix your issue, you should clear the bad value out with removeItem.

为了解决你的问题,你应该用removeItem清除坏值。

localStorage.removeItem("customerDatabase");