1

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));
  }
1
  • if city's value is null, you can do this: data.city ?? ''. If city's value is a String called 'undefined', then you can use an if statement. String city = data.city; if (city == 'undefined') city = ''; Commented Dec 1, 2021 at 6:49

3 Answers 3

1

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}`,
Sign up to request clarification or add additional context in comments.

Comments

1

use OR || operator if city is undefined

Address: data.address + ", " + data.city || "" + ", " + data.provice

Comments

0

You can use nullish coalescing operator.

data.city ?? ""

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.