Assignment to constant variable exception

那年仲夏 提交于 2020-08-20 15:45:10

问题


I am getting the following exception: Value currently is [object Object] popup.html:1 Error handling response: TypeError: Assignment to constant variable. at chrome-extension://....../popup.js:19:18 Trying to get the input from my Chrome extension and using it in here...

const settings =    {
                        name: "",
                        email: "",
                        tel: "",
                        cc_number: "",
                        cc_exp: "",
                        cc_csc: "",
                        address_line1: "",
                        address_level2: "",
                        address_level1: "",
                        postal_code: ""
                    }


function getSettingsfromStorage() {
    chrome.storage.local.get(['settings'], function(result) {
        console.log('im getter');
        console.log('Value currently is ' + result); //Exception
        settings = result; //Exception
    });
}

function saveSettingstoStorage() {
    chrome.storage.local.set({'settings': settings}, function() {
        console.log('Value is set to ' + settings);
    });
}

function getSettingsfromPopup() {
    for(let setting in settings) {
        console.log(setting);
        settings.setting = document.getElementById(setting).value;
        console.log(setting);
    }
}

function saveSettingstoPopup() {
    for(let setting in settings) {
        console.log(setting);
        if(document.getElementById(setting)) {
            document.getElementById(setting).value = settings.setting;
        }
        console.log(setting);
    } 
}

async function getFromPopUpAndSaveToStorage() {
    await getSettingsfromPopup();
    saveSettingstoStorage();
}

async function getFromStorageAndSaveToSettings() {
    await getSettingsfromStorage();
    saveSettingstoPopup();
}

document.addEventListener('DOMContentLoaded', () => {
    //settings.fullName = document.getElementById(name).value;
    getFromStorageAndSaveToSettings();
    document.getElementById("saveButton").addEventListener("click", getFromPopUpAndSaveToStorage());
})   

回答1:


Use let instead of const.

You can't reassign a variable which declared as const, if it is an object, you can change its properties. If it is an array, you can change its values, but still you can't reassign it.



来源:https://stackoverflow.com/questions/60688646/assignment-to-constant-variable-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!