最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot read properties of undefined (reading 'split') - Stack Overflow

programmeradmin3浏览0评论

I am trying to use 'split' in nodejs and I am getting can not read property of undefined (reading 'split') error. Does anybody have a clue how I can solve this? Here is a snippet of my code. I am using node version 16.13.1.

***var config = require('./decrypt.js');
var temp = config.replace('{','');
temp = temp.replace('}','');
temp = temp.split(',');
var userArr=temp[0];
var passArr=temp[1];
var serverArr=temp[2];
var dbArr=temp[3];
var usertemp = userArr.split(':');
var passtemp = passArr.split(':');
var servertemp = serverArr.split(':');
var dbtemp = dbArr.split(':');***

I am trying to use 'split' in nodejs and I am getting can not read property of undefined (reading 'split') error. Does anybody have a clue how I can solve this? Here is a snippet of my code. I am using node version 16.13.1.

***var config = require('./decrypt.js');
var temp = config.replace('{','');
temp = temp.replace('}','');
temp = temp.split(',');
var userArr=temp[0];
var passArr=temp[1];
var serverArr=temp[2];
var dbArr=temp[3];
var usertemp = userArr.split(':');
var passtemp = passArr.split(':');
var servertemp = serverArr.split(':');
var dbtemp = dbArr.split(':');***
Share Improve this question asked Feb 18, 2022 at 8:10 nardosnardos 631 gold badge1 silver badge5 bronze badges 4
  • What is the value of config? – Samuel Liew Commented Feb 18, 2022 at 8:11
  • Split is a method of String. – Master.Deep Commented Feb 18, 2022 at 8:12
  • Wele to Stack Overflow! Please visit the help center, take the tour to see what and How to Ask. If you get stuck, post a minimal reproducible example of your attempt, noting input and expected output using the [<>] snippet editor. – mplungjan Commented Feb 18, 2022 at 8:36
  • Based on the magics with {, }, ,, and :, you are probably looking for something like var temp=JSON.parse(config);. That strange file is probably a JSON, see developer.mozilla/en-US/docs/Learn/JavaScript/Objects/JSON or json – tevemadar Commented Feb 19, 2022 at 13:45
Add a ment  | 

2 Answers 2

Reset to default 0

You can debug your code or log it to the browser's, e.g. Chrome, dev console to determine which variable is undefined.

var config = require('./decrypt.js');
var temp = config.replace('{','');
temp = temp.replace('}','');
console.log({ config : config, temp: temp });
temp = temp.split(',');
console.log(temp);

You cannot use split on array, its for String.

You can only use it on a String

发布评论

评论列表(0)

  1. 暂无评论