0

I am having a problem retrieving data from or writing to my database. The initialization of the table is successful but if I put any other function I get the following warning and the code doesn't work

    export const getDataFromDB = async () => {

  const dispatch = useDispatch();

  try {
    const db = await getDB();
    await db.transaction(async tx => {
      await tx.executeSql(
        'SELECT UserName, Password, RememberMe, MapView, DrawEdits, NetworkIP, Port, Auth FROM UsersInformation',
        [],
        (tx, results) => {
          console.log('Database readed');
          var len = results.rows.length;
          if (len > 0) {
            let item = results.rows.item(0);
            dispatch(setUsername(item.UserName));
            dispatch(setPassword(item.Password));
            dispatch(setRememberMe(item.RememberMe));

            dispatch(setMapView(item.MapView));

            dispatch(drawEdits(item.DrawEdits));

            dispatch(networkIp(item.NetworkIP));
            dispatch(networkPort(item.Port));
            dispatch(networkAuth(item.Auth));
          }
        },
      );
    });
  } catch (error) {
    console.log(error.message);
  }
};

The error is the following

Possible Unhandled Promise Rejection (id: 3): Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:

  1. You might have mismatching versions of React and the renderer (such as React DOM)
  2. You might be breaking the Rules of Hooks
  3. You might have more than one copy of React in the same app See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem. Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
  4. You might have mismatching versions of React and the renderer (such as React DOM)
  5. You might be breaking the Rules of Hooks
  6. You might have more than one copy of React in the same app See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
  • One of those functions, probably getDb, is a hook, and it's being called outside the body of a functional component. If you search for the error you should find a wealth of information on this topic. – Abe May 26 '22 at 20:39
  • Does this answer your question? [Invalid hook call. Hooks can only be called inside of the body of a function component](https://stackoverflow.com/questions/56663785/invalid-hook-call-hooks-can-only-be-called-inside-of-the-body-of-a-function-com) – Abe May 26 '22 at 20:39
  • @Abe No, I wish it did, but it didn't solve my problem. I will keep my eyes open for a solution. – Julian Franco May 26 '22 at 22:26
  • It's useDispatch. You have to move useDispatch out of the body of the function and into the body of the function component. – Abe May 27 '22 at 02:17
  • Thanks @Abe that was the error. I have been able to solve the problem. – Julian Franco May 28 '22 at 18:05

0 Answers0