【报错】 “TypeError: Cannot read properties of undefined (reading ‘split‘)“

简介: 【报错】 “TypeError: Cannot read properties of undefined (reading ‘split‘)“

报错问题

在使用 split()方法分割成字符串时,报红信息如下:Cannot read properties of undefined (reading ‘split‘)

报错分析

在对数据进行分割以前,要先判断当前数据是否存在为空的情况;如果不对数据进行判断,可能会导致异常。所以在调用 splid() 函数前,需要对当前需要进行分割的数据进行非空的判断(或者是当数据不为空的情况下调用)。

报错解决

在对数据进行分割以前,要先判断当前数据是否存在为空的情况;如果不对数据进行判断,可能会导致异常。所以在调用 splid() 函数前,需要对当前需要进行分割的数据进行非空的判断(或者是当数据不为空的情况下调用)。

const imgUrl = "https://tu.cloud.cn/flower/flower-size.jpg?size=1800";
// 当数据不为空的情况下
if(imgUrl) {
   
  // 截取 再拼接新的字符串
  const newImgUrl = imgUrl.split('?')[0] + "?size=600";
  console.log(newImgUrl);  //https://tu.cloud.cn/flower/flower-size.jpg?size=600
}
相关文章
|
2天前
|
小程序 前端开发 API
【微信小程序】TypeError: Cannot read property ‘get‘ of undefined & Error: MiniProgramError
【微信小程序】TypeError: Cannot read property ‘get‘ of undefined & Error: MiniProgramError
|
2天前
|
资源调度 前端开发
编译第三方前端项目时候出现Syntax Error: TypeError: Cannot set properties of undefined (setting ‘parent‘)
编译第三方前端项目时候出现Syntax Error: TypeError: Cannot set properties of undefined (setting ‘parent‘)
51 0
|
1天前
webpack版本问题 Cannot read property ‘createHash‘ of undefined
webpack版本问题 Cannot read property ‘createHash‘ of undefined
|
2天前
|
iOS开发 MacOS
TypeError: Cannot read property ‘shop‘ of undefined
TypeError: Cannot read property ‘shop‘ of undefined
7 0
|
2天前
|
机器学习/深度学习
TypeError: Cannot read property ‘name‘ of undefined at Qe.onShow
TypeError: Cannot read property ‘name‘ of undefined at Qe.onShow
11 0
|
2天前
|
JavaScript
报错 Cannot read properties of undefined(reading‘addEventListener‘)如何解决
报错 Cannot read properties of undefined(reading‘addEventListener‘)如何解决
35 1
|
2天前
|
小程序 开发者
【Hbuilder】Hbuilder Cannot read property ‘forceUpdate‘ of undefined
【Hbuilder】Hbuilder Cannot read property ‘forceUpdate‘ of undefined
19 0
|
2天前
|
缓存 JavaScript
报错:cannot read properties of undefined “reading url“
报错:cannot read properties of undefined “reading url“
|
2天前
|
JavaScript
JS中Null和Undefined的区别及用法
JS中Null和Undefined的区别及用法
16 1
|
2天前
|
JavaScript 前端开发 算法
undefined与null的区别
在JavaScript中,undefined和null都表示变量未被赋值或值缺失,但它们在使用场景上有一些区别。 - **`语义不同`**:undefined表示一个变量未被赋值或者声明后未进行初始化。而null表示一个变量被明确地设置为无值或者表示空值的概念。 - **`类型不同`**:undefined是一种基本数据类型,而null是一个引用类型。 - **`条件判断`**:在条件判断中,使用if (variable === undefined)或者if (variable === null)可以进行区分。