简体   繁体   English

Javascript:TypeError:无法将未定义或null转换为对象

[英]Javascript: TypeError: Cannot convert undefined or null to object

I'm reading a text file and converting it to JSON format using regex in my react project.It is working fine but when including in my code it is giving TypeError: Cannot convert undefined or null to object There is some problem while performing replace but I am unable to understand the problem. 我正在读取文本文件并将其在我的react项目中使用正则表达式将其转换为JSON格式。它可以正常工作,但是当包含在我的代码中时,它会给出TypeError:无法将undefined或null转换为对象执行替换时存在一些问题,但是我无法理解问题。 Please help me out in resolving this issue. 请帮助我解决此问题。

I think the problem is allText = allText.replace(/\\s{2,}|\\n+/g, " "); 我认为问题是allText = allText.replace(/\\s{2,}|\\n+/g, " "); is not getting completed. 还没有完成。

.match(regex) returns null if there are no matches and will cause an error when trying to perform the spread operation. 如果没有匹配项, .match(regex)将返回null ,并且在尝试执行传播操作时将导致错误。

you could use [...text.match(regex)||[]] but that's pretty ugly. 您可以使用[...text.match(regex)||[]]但这很丑陋。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:无法将未定义或空值转换为对象 javascript - TypeError : Cannot convert undefined or null to object javascript JavaScript 未捕获类型错误:无法将未定义或 null 转换为 object - JavaScript Uncaught TypeError: Cannot convert undefined or null to object 错误TypeError:无法将未定义或null转换为对象 - error TypeError: Cannot convert undefined or null to object 未捕获的TypeError:无法将未定义或null转换为对象 - Uncaught TypeError: Cannot convert undefined or null to object Sequelize TypeError:无法将未定义或空值转换为对象 - Sequelize TypeError: Cannot convert undefined or null to object 类型错误:无法将 undefined 或 null 转换为对象 - TypeError: Cannot convert undefined or null to object TypeError:无法在反应中将未定义或 null 转换为 object - TypeError: Cannot convert undefined or null to object in react 无法在 javascript 中将未定义或 null 转换为 object - Cannot convert undefined or null to object in javascript javascript 无法将 undefined 或 null 转换为 object 问题 - javascript cannot convert undefined or null to object question 对于给出 TypeError 的空对象:无法将 undefined 或 null 转换为对象 - For empty object giving the TypeError: Cannot convert undefined or null to object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM