I have this in the firestore where "1" is a map with selected. If it exist, it won't cause any error. But if it does not exist, it says that .join is not a function.
user[1]?.selected?.join(", ")
This is what it looks like in firestore
Will this be fine, I've made a workaround on this one?
{user[1]?.selected ? (
<>
{" "}
<ListItemText
primary={user[1]?.selected.join(", ")}
/>
</>
) : (
<></>
)}
Your issue is because - even if it exists, it's not an Array.
Has to be an Array in order to use the Array.prototype.join()
const user = {
1: {selected: [1, 2, 4]},
2: {},
3: {selected: "test this"}, // << Uncaught TypeError
};
console.log( user[1]?.selected?.join(", ") ); // "1, 2, 4"
console.log( user[2]?.selected?.join(", ") ); // undefined
console.log( user[3]?.selected?.join(", ") ); // Uncaught TypeError
so, Uncaught TypeError because you cannot use .join() on String primitive