问题
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