TypeError: Cannot read property 'replace' of undefined

Muhammad Daniyal Raza picture Muhammad Daniyal Raza · Aug 29, 2017 · Viewed 14.9k times · Source

Error

  • Seneca Fatal Error ==================

    Message: seneca: Action cmd:signup,role:api failed: Cannot read property 'replace' of undefined.

    Code: act_execute

    Details: { message: 'Cannot read property \'replace\' of undefined', pattern: 'cmd:signup,role:api', fn: [Function: signup], cb: [Function], instance: 'Seneca/pr061njfdnng/1504005334341/1/3.2.2/-', 'orig$': TypeError: Cannot read property 'replace' of undefined at /usr/src/app/services/common.js:22:24 at Array.forEach (native) at module.exports (/usr/src/app/services/common.js:20:13) at Seneca.signup (/usr/src/app/services/api.js:117:17) at execute_action (/usr/src/app/node_modules/seneca/seneca.js:1060:20) at Object.act_fn [as fn] (/usr/src/app/node_modules/seneca/seneca.js:1013:11) at Immediate.processor (/usr/src/app/node_modules/gate-executor/gate-executor.js:136:14) at runCallback (timers.js:781:20) at tryOnImmediate (timers.js:743:5) at processImmediate [as _immediateCallback] (timers.js:714:5), 'message$': 'Cannot read property \'replace\' of undefined', plugin: {} }

    Stack: at /usr/src/app/services/common.js:22:24 at Array.forEach (native) at module.exports (/usr/src/app/services/common.js:20:13) at Seneca.signup (/usr/src/app/services/api.js:117:17) at execute_action (/usr/src/app/node_modules/seneca/seneca.js:1060:20) at Object.act_fn [as fn] (/usr/src/app/node_modules/seneca/seneca.js:1013:11) at Immediate.processor (/usr/src/app/node_modules/gate-executor/gate-executor.js:136:14) at runCallback (timers.js:781:20) at tryOnImmediate (timers.js:743:5) at processImmediate [as _immediateCallback] (timers.js:714:5)

Code

if(queryString.indexOf('?') > -1){
        queryString = queryString.split('?')[1];
      }
      var pairs = queryString.split('&');

      pairs.forEach(function(pair) {
        pair = pair.split('=');      
        var s = pair[1].replace(/\+/g," ");
        //console.log('s', s);
        //console.log('pair[1]', pair[1]);
        var sfinal = decodeURIComponent(s || '');
        //console.log('sfinal', sfinal);

        result[pair[0]] = sfinal;
      });
      //done( null, result )  

Answer

udbhavs picture udbhavs · Aug 29, 2017

If there is no '=' in pair, the array returned will have a single element and index 1 will returned undefined. Maybe you should check that.