rhiya
rhiya

Reputation: 13

js: TypeError: Cannot read property 'replace' of undefined using

I wanted to replace /\r?\n|\r/g with empty string but got a type error "Cannot read property 'replace' of undefined".

export function getCupsCredentials() {
    var vcap_services = process.env.VCAP_SERVICES;
    var vcap_servces_as_single_line = vcap_services.replace(/\r?\n|\r/g, " ");
    var vcap_servces_as_json = JSON.parse(vcap_servces_as_single_line)
    var vcap_servces_user_provided = vcap_servces_as_json['user-provided']
    var cups_credentials = vcap_servces_user_provided[0].credentials;
    return cups_credentials;
}

i got the following error message on using .replace

TypeError: Cannot read property 'replace' of undefined
at getCupsCredentials (/app/ui/dist/webpack:/src/utils/VcapUtils.js:4:50)
at Object.defineProperty.value (/app/ui/dist/webpack:/src/utils/IAMUtils.js:5:17)
at __webpack_require__ (/app/ui/dist/webpack:/webpack/bootstrap 713b700ecf31:19:1)
at Object.defineProperty.value (/app/ui/dist/webpack:/src/api/middleware/filters/authFilter.js:1:1)
at __webpack_require__ (/app/ui/dist/webpack:/webpack/bootstrap 713b700ece831:19:1)
at Object.<anonymous> (/app/ui/dist/webpack:/src/api/middleware/pre.js:1:1)
at __webpack_require__ (/app/ui/dist/webpack:/webpack/bootstrap 713b70031:19:1)
at Object.defineProperty.value (/app/ui/dist/webpack:/src/api/middleware/index.js:1:1)
at __webpack_require__ (/app/ui/dist/webpack:/webpack/bootstrap 713b700ececf31:19:1)
at Object.canUseDOM (/app/ui/dist/webpack:/src/server.js:4:1)
Starting Conductor UI
using Conductor API server from 'http://chand-wf-nfce-server.cloud.pcftest.com/api'
 
/app/ui/dist/webpack:/src/utils/VcapUtils.js:4
var vcap_servces_as_single_line = vcap_services.replace(/\r?\n|\r/g, " ");

Upvotes: 0

Views: 5666

Answers (2)

Ketan Ramteke
Ketan Ramteke

Reputation: 10655

The most probable cause is that your process.env.VCAP_SERVICES is not initialized properly and it is initializing your vcap_services with undefined. As the replace method does not work with undefined thus that error.

Try checking if vcap_services is empty or not by the ternary operator

export function getCupsCredentials() {
  var vcap_services = process.env.VCAP_SERVICES;
  var vcap_servces_as_single_line = vcap_services
    ? vcap_services.replace(/\r?\n|\r/g, " ")
    : "vcap_servisec is empty";
  console.log(vcap_services);
  /*
    var vcap_servces_as_json = JSON.parse(vcap_servces_as_single_line)
    var vcap_servces_user_provided = vcap_servces_as_json['user-provided']
    var cups_credentials = vcap_servces_user_provided[0].credentials;
    return cups_credentials;
    */
}

Upvotes: 1

Vyom Desai
Vyom Desai

Reputation: 61

If these values are getting fetched from a remote server then the case might be that you are trying to replace the characters before the data is assigned to the variable. Try using await functions or the .then() And also replace as such var vcap_servces_as_single_line = vcap_services?.replace(/\r?\n|\r/g, " ");

So that the code will not scream at you if you have an undefined value.

Upvotes: 1

Related Questions