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?

letsCode
  • 2,774
  • 1
  • 13
  • 37

2 Answers2

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)
              }))
            }
          })
        }
  };
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
  • @soldfor see I have updated. Actually you are missing two `return` statements – Maheer Ali Mar 12 '19 at 16:51
  • 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. – letsCode Mar 12 '19 at 16:52
  • 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`. – Subhendu Kundu Mar 12 '19 at 16:55
  • @soldfor I have added explanation why you need `return` – Maheer Ali Mar 12 '19 at 17:00
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.

Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231