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 likevar 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
2 Answers
Reset to default 0You 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