我正在尝试使用 RTK 查询通过异步函数设置 contactSlice.ts 的初始状态。
我已阅读文档并在线搜索,但没有找到解决此问题的合适解决方案。
联系人API.ts:
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
import { Contact } from '../models/contact.model';
export const contactsApi = createApi({
reducerPath: "contactsApi",
baseQuery: fetchBaseQuery({ baseUrl: "http://localhost:3006/" }),
tagTypes: ['Contact'],
endpoints: (builder) => ({
contacts: builder.query<Contact[], void>({
query: () => '/contacts',
providesTags: ['Contact']
}),
contact: builder.query<Contact, string>({
query: (id) => `/contacts/${ id }`,
providesTags: ['Contact']
}),
addContact: builder.mutation<{}, Contact>({
query: contact => ({
url: '/contacts',
method: 'POST',
body: contact
}),
invalidatesTags: ['Contact']
}),
updateContact: builder.mutation<void, Contact>({ …Run Code Online (Sandbox Code Playgroud)我正在尝试从连接到 MongoDB 的服务器端获取客户端数据。
我在前端使用 React,并使用 Axios 处理 HTTP 请求。
我有 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;
});
Run Code Online (Sandbox Code Playgroud)
}
index.jsx 文件:
import { getNotesFromDB as getNotesFromAPI …Run Code Online (Sandbox Code Playgroud) reactjs ×2
axios ×1
frontend ×1
javascript ×1
node.js ×1
react-redux ×1
redux ×1
redux-thunk ×1