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");
}
}
}