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


0 votes
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("", {
      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(() => {
      .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("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