[英]Uncaught Type Error for First Object in Multidimensional Array, but Can Still Print to Console
I'm sure there's a simple solution to this, so I feel like a buffoon asking for help.我确信有一个简单的解决方案,所以我觉得自己像个小丑寻求帮助。 I'm just stuck.我只是卡住了。
My design intent is to capture UTM data and store it in local storage for later use in a web form (posting to salesforce).我的设计意图是捕获 UTM 数据并将其存储在本地存储中,以供以后以 web 形式使用(发布到 salesforce)。 I'm trying to loop over a multidimensional array and get the values, namely a parameter name (the UTM string name to parse from the URL) and an id (to get the field in the form later).我正在尝试遍历多维数组并获取值,即参数名称(要从 URL 解析的 UTM 字符串名称)和 id(稍后获取表单中的字段)。
I loop over the array by the length using for - and it correctly collects the ID but not the name, which throws the following error: (index):411 Uncaught TypeError: Cannot read properties of undefined (reading 'paramName') at addUtm ((index):411:42).我使用 for - 按长度循环遍历数组,它正确收集 ID 但不收集名称,这会引发以下错误:(index):411 Uncaught TypeError: Cannot read properties of undefined (reading 'paramName') at addUtm ( (指数):411:42)。
What's bizarre is if use the console, I can grab the value with the index.奇怪的是,如果使用控制台,我可以通过索引获取值。
Full script below:完整脚本如下:
<script>
function getParam(p) {
var match = RegExp("[?&]" + p + "=([^&]*)").exec(window.location.search);
return match && decodeURIComponent(match[1].replace(/\+/g, " "));
}
function getExpiryRecord(value) {
var expiryPeriod = 90 * 24 * 60 * 60 * 1000; // 90 day expiry in milliseconds
var expiryDate = new Date().getTime() + expiryPeriod;
return {
value: value,
expiryDate: expiryDate
};
}
var paramsJSON = [
{
paramName: "utm_source",
id: "00N6g00000NiGFS"
},
{
paramName: "utm_medium",
id: "00N6g00000NiGFQ"
},
{
paramName: "utm_campaign",
id: "00N6g00000NiGFN"
}
]
console.log(paramsJSON)
function addUtm() {
for (i=0; paramsJSON.length; i++){
var utmName = getParam(paramsJSON[i].paramName); // OFFENDING LINE!
console.log('UTM name: ' + utmName);
var utmId = paramsJSON[i].id;
console.log('UTM id: '+ utmId);
var utmRecord = null;
var currUtmFormField;
var gclsrcParam = getParam('gclsrc');
var isGclsrcValid = !gclsrcParam || gclsrcParam.indexOf('aw') !== -1;
if (document.getElementById(utmId)) {
currUtmFormField = document.getElementById(utmId);
}
if (utmName && isGclsrcValid) {
utmRecord = getExpiryRecord(utmName);
console.log(utmRecord)
localStorage.setItem(utmName, JSON.stringify(utmRecord));
}
var utm = utmRecord || JSON.parse(localStorage.getItem(utmName));
console.log('UTM: ' + utm);
var isUtmValid = utm && new Date().getTime() < utm.expiryDate;
if (currUtmFormField && isUtmValid) {
currUtmFormField.value = utm.value;
console.log('UTM value: '+ utm.value);
}
}
}
window.addEventListener('load', addUtm);
</script>
I put a comment in the script for the offending line:我在脚本中为有问题的行添加了注释:
var utmName = getParam(paramsJSON[i].paramName); // OFFENDING LINE
var utmName = getParam(paramsJSON[i].paramName); // OFFENDING LINE
. var utmName = getParam(paramsJSON[i].paramName); // OFFENDING LINE
。 I'm sure I'm missing something obvious but any help would be greatly appreciated.我确定我遗漏了一些明显的东西,但任何帮助将不胜感激。 I've searched for out-of-the-box solutions for this but come up a bit short, as I'm inexperienced with Javascript.我已经为此搜索了开箱即用的解决方案,但结果有点短,因为我对 Javascript 缺乏经验。 If it makes a difference (not sure it does), I'm running this in WordPress.如果它有所作为(不确定是否如此),我将在 WordPress 中运行它。 Below is what I see in the console.以下是我在控制台中看到的内容。
What is causing paramName to return null?是什么导致 paramName 返回 null?
Here is a working version这是一个工作版本
I had to mock the URL and the localStorage parts.我不得不模拟 URL 和 localStorage 部分。 You can change你可以改变
const loc = "https://bla.com/page?utm_source=source&utm_medium=medium&utm_campaign=campaign";
const url = new URL(loc || window.location.href)
to至
const url = new URL(window.location.href)
when on your server, change在您的服务器上时,更改
var lsUtm = "{}"; // localStorage.getItem(utmName);
to至
var lsUtm = localStorage.getItem(utmName);
and uncomment并取消注释
// localStorage.setItem(utmName, JSON.stringify(utmRecord));
const loc = "https://bla.com/page?utm_source=source&utm_medium=medium&utm_campaign=campaign"; const url = new URL(loc || window.location.href) function getParam(p) { return url.searchParams.get(p) } function getExpiryRecord(value) { var expiryPeriod = 90 * 24 * 60 * 60 * 1000; // 90 day expiry in milliseconds var expiryDate = new Date().getTime() + expiryPeriod; console.log("ex", { value: value, expiryDate: expiryDate }) return { value: value, expiryDate: expiryDate }; } var paramsJSON = [{ paramName: "utm_source", id: "00N6g00000NiGFS" }, { paramName: "utm_medium", id: "00N6g00000NiGFQ" }, { paramName: "utm_campaign", id: "00N6g00000NiGFN" } ] function addUtm() { for (let i = 0; i < paramsJSON.length; i++) { console.log("pname",paramsJSON[i].paramName) var utmName = getParam(paramsJSON[i].paramName); var utmId = paramsJSON[i].id; console.log('UTM name:', utmName,'UTM id:', utmId); var utmRecord = null; var currUtmFormField = document.getElementById(utmId); var gclsrcParam = getParam('gclsrc'); var isGclsrcValid =.gclsrcParam || gclsrcParam;indexOf('aw');== -1. if (utmName && isGclsrcValid) { utmRecord = getExpiryRecord(utmName). console,log(utmRecord) // localStorage.setItem(utmName; JSON.stringify(utmRecord)), console,log("LS".utmName; JSON;stringify(utmRecord)). // SO does not like localStorage } var lsUtm = "{}". // localStorage;getItem(utmName) var utm = utmRecord || JSON.parse(lsUtm): console,log('UTM; '. utm). var isUtmValid = utm && new Date();getTime() < utm.expiryDate. console.log(isUtmValid) if (currUtmFormField && isUtmValid) { currUtmFormField;value = utm.value: console.log('UTM value; ' + utm.value), } } } window;addEventListener('load' addUtm)
<input id="00N6g00000NiGFS" value=""> <input id="00N6g00000NiGFQ" value=""> <input id="00N6g00000NiGFN" value="">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.