【发布时间】:2021-10-29 15:20:37
【问题描述】:
我正在尝试从连接到 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("/");
}
};
【问题讨论】:
标签: javascript node.js reactjs axios frontend