当在另一个变量中使用时,变量会一直返回一个函数
Variable keeps returning a function when used inside another variable
const getPatientProceduresID = createAsyncThunk(
"selectedPatient/getPatientProcedures",
async (arg, { getState }) => {
const state = getState();
const patient = state.selectedPatient.patient;
const token = state.userDetails.token;
try {
const apiUrl = process.env.REACT_APP_API_URL;
var config = {
method: "get",
url: `${apiUrl}/GetProcedures/${patient}`,
headers: {
accept: "application/json",
"Authorization-Token": token,
},
};
const response = await axios(config);
const data = await response.data;
return data[0].id;
} catch (error) {
console.log(error);
}
}
);
export const getPatientProcedureDetails = createAsyncThunk(
"selectedPatient/getPatientProcedureDetails",
async (arg, { getState }) => {
const state = getState();
const patientID = state.selectedPatient.patient;
const procedureID = getPatientProceduresID;
console.log(procedureID());
const token = state.userDetails.token;
try {
const apiUrl = process.env.REACT_APP_API_URL;
var config = {
method: "get",
url: `${apiUrl}/GetProcedureDetail/${patientID}/${procedureID}`,
headers: {
accept: "application/json",
"Authorization-Token": token,
},
};
const response = await axios(config);
const data = await response.data;
return data;
} catch (error) {
console.log(error);
}
}
);
当我 console.log procedureID 时,它 returns 整个函数而不是结果。我究竟做错了什么?
我需要在第二个变量中使用第一个变量的结果,因为它包含我需要的过程 ID“${apiUrl}/GetProcedureDetail/${patientID}/${procedureID}”
您正在将函数 getPatientProceduresID
分配给变量 procedureID
而未执行。
此外,它是一个 asyncThunk,因此您需要分派它 - 如果您直接想要结果,还需要将其解包。
所以:
const procedureID = dispatch(getPatientProceduresID()).unwrap();
const getPatientProceduresID = createAsyncThunk(
"selectedPatient/getPatientProcedures",
async (arg, { getState }) => {
const state = getState();
const patient = state.selectedPatient.patient;
const token = state.userDetails.token;
try {
const apiUrl = process.env.REACT_APP_API_URL;
var config = {
method: "get",
url: `${apiUrl}/GetProcedures/${patient}`,
headers: {
accept: "application/json",
"Authorization-Token": token,
},
};
const response = await axios(config);
const data = await response.data;
return data[0].id;
} catch (error) {
console.log(error);
}
}
);
export const getPatientProcedureDetails = createAsyncThunk(
"selectedPatient/getPatientProcedureDetails",
async (arg, { getState }) => {
const state = getState();
const patientID = state.selectedPatient.patient;
const procedureID = getPatientProceduresID;
console.log(procedureID());
const token = state.userDetails.token;
try {
const apiUrl = process.env.REACT_APP_API_URL;
var config = {
method: "get",
url: `${apiUrl}/GetProcedureDetail/${patientID}/${procedureID}`,
headers: {
accept: "application/json",
"Authorization-Token": token,
},
};
const response = await axios(config);
const data = await response.data;
return data;
} catch (error) {
console.log(error);
}
}
);
当我 console.log procedureID 时,它 returns 整个函数而不是结果。我究竟做错了什么? 我需要在第二个变量中使用第一个变量的结果,因为它包含我需要的过程 ID“${apiUrl}/GetProcedureDetail/${patientID}/${procedureID}”
您正在将函数 getPatientProceduresID
分配给变量 procedureID
而未执行。
此外,它是一个 asyncThunk,因此您需要分派它 - 如果您直接想要结果,还需要将其解包。
所以:
const procedureID = dispatch(getPatientProceduresID()).unwrap();