1

I am trying to check whether string contain specific given sentence in string or not. I have below string in that I want to check 'already registered' sentence present or not however string is contain array so I couldn't get.

message =  'User error fabric-ca request register failed with errors [[{"code":0,"message":"Registration of \'yandgsub15\' failed: Identity \'yandgsub15\' is already registered"}]]' }  

I tried below function, however I couldn't get it though it. please anybody have solution?

var n = error.includes("registered");
6
  • 2
    message.indexOf("already registered") >= 0 Commented Oct 20, 2020 at 7:19
  • You used the incorrect variable to get your expected result. Try to change error to message. var n = message.includes("registered"); Commented Oct 20, 2020 at 7:21
  • your string name is 'message' but you're searching your word in a string named error, your first real problem is here Commented Oct 20, 2020 at 7:22
  • right I forget to do it like const message = error.message Commented Oct 20, 2020 at 7:26
  • var n = message.includes("registered"); Commented Oct 20, 2020 at 7:28

2 Answers 2

3

You are trying to search for "registered" in a string called error but there isn't try this:

message =  'User error fabric-ca request register failed with errors [[{"code":0,"message":"Registration of \'yandgsub15\' failed: Identity \'yandgsub15\' is already registered"}]]' }

var n = message.includes("registered");
Sign up to request clarification or add additional context in comments.

1 Comment

right I forget to do it like const message = error.message
2

I think you just don't search on the good variable

In your example you're trying to search "registered" on error and not on message

The code below is working

message = 'User error fabric-ca request register failed with errors [[{"code":0,"message":"Registration of \'yandgsub15\' failed: Identity \'yandgsub15\' is already registered"}]]' 

var n = message.includes('registered')

console.log(n) // true

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.