简体   繁体   中英

React Native TypeError undefined is not a function (near '…data.map…')

I'm trying to load data from firebase in react native into a table but I keep getting this error. It works fine with any two dimensional array but when I load the 2d array from firebase the error comes up.

async function get_passwords(user) {
  const check_user = db.collection("users").doc(user);

  const doc = await check_user.get();

  const entries = Object.entries(doc.data());

  return entries;
}
function InfoScreen({ navigation, route }) {
  const { param1, param2 } = route.params;
  const stated = {
    tableHead: ["Info", "Password"],

    tableData: [
      ["1", "2"],
      ["a", "b"],
      ["1", "2"],
      ["a", "b"],
    ],
  };

  let entries = get_passwords(param1);

  return (
    <View>
      <Table>
        <Row data={stated.tableHead} />
        <Rows data={entries} />
      </Table>
    </View>
  );
}

Since get_passwords is an async function, you need to use "await" for calling a function like

let entries = await get_passwords(param1);

with useState and useEffect

 const [entries, setEntries] = useState([]);

  useEffect(() => {
    getEntries(param1);
  }, [param1]);

  const getEntries = async (param) => {
    const passwords = await get_passwords(param);
    setEntries(passwords);
  } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM