0

I have the following function

  churnModel = () => {
        if (this.props.churnModel === undefined || this.props.churnModel.length === 0) {
          return("N/A")
        } else {
          this.props.churnModel.map((churn) => {
            if (churn === undefined || churn.length === 0) {
              return("N/A")
            } else {
              return(churn.map((model) => {
                this.service(model.scoreModelId)
              }))
            }
          })
        }
  };

The this.service functions looks like this...

service(e) {
  switch(e.toString().toLowerCase()) {
    case "in":
      return <span>in</span>
    case "rpt_callr":
      return <span>repeat</span>
    default:
      return <span>na</span>
  }
}

I am expecting to display the result in here:

<div className="riskScore">{this.churnModel()}</div>

Nothing gets displayed, but when I put in logs, those get printed.

What is happening here?

4
  • 2
    You missed the return statement inside map. It should be return this.service(model.scoreModelId) Commented Mar 12, 2019 at 16:48
  • doesnt matter... still doesnt work. Commented Mar 12, 2019 at 16:49
  • codesandbox.io/s/kxn064w85v Commented Mar 12, 2019 at 16:55
  • Check this out, I created a working demo resolving your issue Commented Mar 12, 2019 at 16:56

2 Answers 2

1

you need to put return before this.props.churnModel.map.this.service(model.scoreModelId)

  1. A function will return undefined if nothing is nothing is returned.
  2. map() takes a callback and changes each element of array to return value of the that callback. If you don't return anything all elements will be undefined

You can also get rid of return before this.service(model.scoreModelId) by removing {}.Like this.

return(churn.map((model) => this.service(model.scoreModelId)))

Here is the code

churnModel = () => {
        if (this.props.churnModel === undefined || this.props.churnModel.length === 0) {
          return("N/A")
        } else {
          return this.props.churnModel.map((churn) => {
            if (churn === undefined || churn.length === 0) {
              return("N/A")
            } else {
              return(churn.map((model) => {
                return this.service(model.scoreModelId)
              }))
            }
          })
        }
  };
Sign up to request clarification or add additional context in comments.

4 Comments

@soldfor see I have updated. Actually you are missing two return statements
the added return worked... why do I have to return this and that and that and even that? seems silly to "return" within a return.
So you can avoid adding return if you use a arrow function like return(churn.map((model) => this.service(model.scoreModelId))). These all are function, if you wanna have a value to the upper scope you need to return the value. I a way you are saying when I call you return me something.
@soldfor I have added explanation why you need return
0

You have to use return statement in couple of lines:

 churnModel = () => {
            if (this.props.churnModel === undefined || this.props.churnModel.length === 0) {
              return("N/A")
            } else {
              return this.props.churnModel.map((churn) => {
                if (churn === undefined || churn.length === 0) {
                  return("N/A")
                } else {
                  return(churn.map((model) => {
                    return this.service(model.scoreModelId)
                  }))
                }
              })
            }
      };

Why do you need return? It's because you're using curly braces.

1 Comment

Whoa, I'm too late to answer just to give proper format to return statement with bold. Learnt how to add bold in formatted code anyways.

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.