使用 AXIOS 在 JS 中未定义异步函数的结果
The result of an async function is undefined in JS using AXIOS
我正在尝试从连接到 MongoDB 的服务器端获取我客户端的数据。
我在前端使用 React,在 HTTP 请求中使用 Axios。
我有 2 个文件,一个用于 API,一个是应用程序的 index.jsx。
我成功地从数据库中获取了数据,但我在 index.jsx 上得到的结果始终未定义。
API 文件:
export async function getNotesFromDB(googleId) {
let answer;
await axios
.get(url + "/note/" + googleId, { withCredentials: true }) //WHEN LOCAL : http://localhost:5000/note/
.then((notesDB) => {
answer = notesDB;
})
.catch((error) => {
//Indicates the client of an error getting the notes from
console.log(error);
answer= null;
})
.finally( () => {
return answer;
});
}
index.jsx 文件:
import { getNotesFromDB as getNotesFromAPI } from "../API/Notes.jsx";
async function getNotesFromDB() {
if (userInfo) {
let googleId = userInfo.googleId;
const result = await getNotesFromAPI(googleId);
console.log(result);
} else {
history.push("/");
}
};
您 return 没有从 getNotesFromDB
函数中获取任何内容,您应该 return axios 调用的结果:
export async function getNotesFromDB(googleId) {
let answer;
return await axios
// Rest of the function body ....
你可以 return 承诺并处理错误
export function getNotesFromDB(googleId) {
return axios
.get(url + "/note/" + googleId, { withCredentials: true }) //WHEN LOCAL : http://localhost:5000/note/
.catch((error) => {
//Indicates the client of an error getting the notes from
console.log(error);
return null
})
}
或
export const getNotesFromDB = (googleId) => axios
.get(url + "/note/" + googleId, { withCredentials: true }) //WHEN LOCAL : http://localhost:5000/note/
.catch((error) => {
//Indicates the client of an error getting the notes from
console.log(error);
return null
})
或者如果您更喜欢使用 async/await
export async function getNotesFromDB(googleId) {
try{
const res = await axios.get(url + "/note/" + googleId, { withCredentials: true })
return res
}catch(e){
console.error(e);
return null;
}
}
我正在尝试从连接到 MongoDB 的服务器端获取我客户端的数据。
我在前端使用 React,在 HTTP 请求中使用 Axios。
我有 2 个文件,一个用于 API,一个是应用程序的 index.jsx。
我成功地从数据库中获取了数据,但我在 index.jsx 上得到的结果始终未定义。
API 文件:
export async function getNotesFromDB(googleId) {
let answer;
await axios
.get(url + "/note/" + googleId, { withCredentials: true }) //WHEN LOCAL : http://localhost:5000/note/
.then((notesDB) => {
answer = notesDB;
})
.catch((error) => {
//Indicates the client of an error getting the notes from
console.log(error);
answer= null;
})
.finally( () => {
return answer;
});
}
index.jsx 文件:
import { getNotesFromDB as getNotesFromAPI } from "../API/Notes.jsx";
async function getNotesFromDB() {
if (userInfo) {
let googleId = userInfo.googleId;
const result = await getNotesFromAPI(googleId);
console.log(result);
} else {
history.push("/");
}
};
您 return 没有从 getNotesFromDB
函数中获取任何内容,您应该 return axios 调用的结果:
export async function getNotesFromDB(googleId) {
let answer;
return await axios
// Rest of the function body ....
你可以 return 承诺并处理错误
export function getNotesFromDB(googleId) {
return axios
.get(url + "/note/" + googleId, { withCredentials: true }) //WHEN LOCAL : http://localhost:5000/note/
.catch((error) => {
//Indicates the client of an error getting the notes from
console.log(error);
return null
})
}
或
export const getNotesFromDB = (googleId) => axios
.get(url + "/note/" + googleId, { withCredentials: true }) //WHEN LOCAL : http://localhost:5000/note/
.catch((error) => {
//Indicates the client of an error getting the notes from
console.log(error);
return null
})
或者如果您更喜欢使用 async/await
export async function getNotesFromDB(googleId) {
try{
const res = await axios.get(url + "/note/" + googleId, { withCredentials: true })
return res
}catch(e){
console.error(e);
return null;
}
}