Home > OS >  Showing undefined value
Showing undefined value

Time:12-01

I have this table where I fetch the data using firestore. There were some cases where this data is empty. I have this address where city is empty it would show undefined. How would I just display it as blank instead of the word "undefined"?

componentDidMount() {
    firestore
      .collection("users")
      .get()
      .then((snapshot) => {
        const users = [];
        snapshot.forEach((doc) => {
          const data = doc.data();
          users.push({
            "User ID": doc.id,
            Address: data.address   ", "   data.city   ", "   data.provice,
                }),
          });
        });
        this.setState({ users: users });
      })
      .catch((error) => console.log(error));
  }

CodePudding user response:

You can use nullish coalescing operator.

data.city ?? ""

CodePudding user response:

Instead of using you can do the following, It will simplify the code readability and cater to your use case.

 Address: `${data.address} , ${data.city || ""} , ${data.provice}`,

CodePudding user response:

use OR || operator if city is undefined

Address: data.address   ", "   data.city || ""   ", "   data.provice
  • Related