Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

javascript - How to fetch data that is just posted from mysql in react-native

I have a mysql database for task with an auto-increment primary key which is the task_ID. Here's the problem. I need the task_ID for other purposes and I'm trying to retrieve it from the database right after a new task is posted into the database. But the hook rules in react native wouldn't let me. The log output from the putInQueue() return undefined which probably means the retrieval of task from the database is done before the new task is posted. Here's my code.

function registerTask() {
    console.log("i am at register screen here!");
    fetch("http://192.168.0.135:3000/new_task", {
      method: "POST",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        task_title: taskTitle,
        task_desc: taskDescription,
        serv_req_ID: "99",
        serv_ctgry_ID: selectedValue,
      }),
    })
      .then((res) => res.json())
      .then((data) => console.log(data))
      .catch((err) => console.log(err));
  }

  useEffect(() => {
    fetch("http://192.168.0.135:3000/task")
      .then((response) => response.json())
      .then((json) => setTask(json))
      .catch((error) => console.error(error));
  });

  function putInQueue() {
    for (var i = 0; i < task.length; i++) {
      if (
        taskTitle == task[i].task_title &&
        taskDescription == task[i].task_desc &&
        selectedValue == task[i].serv_ctgry_ID
      ) {
        taskID = task[i].task_ID;
        console.log(taskID);
        console.log("task ID retrieved");
      }
    }
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...