Search code examples
javascriptnode.jsarraysindexof

Javascript .indexof 'typeError' error despite forcing string conversion


JS drives me insane with issues like this. I have the following code which creates a string (composed of session data and date information) to be written to an array, as such:

var _writes = String(req.session.subscriber + ":" + req.session.postal + "[" + req.session.id + "]=" + _onYear + "-" + _onMonth + "-" + _onDay + "-" + _onHour + "-" + _onMinute);
_users.push(_writes);

Later, I wish to perform an 'indexof' command on the string of the array, as such:

for (_cycle = 0; _cycle < _users.length; ++_cycle) {
_seeks = String(_users[_cycle]);
_score = _seeks.indexof("=");  //ERROR THROWN HERE
//do other stuff here... 
}  //for loop

My error is "TypeError: _seeks.indexof is not a function"...? I thought by converting everything to a string I should be able to perform the 'indexof' command. Can somebody please advise what the issue is here? I thank you in advance.


Solution

  • Probably not a js issue. You are using "indexof" instead of "indexOf" (Uppercase O). Check https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf

    It should be:

    _seeks.indexOf("=");
    

    Don't give up, it will make sense soon :)