Javascript 如何在conductor.productionready.io(假api)中获取我的帖子
我正在使用由提供的文章api进行crud测试[https://conduit.productionready.io/api/articles] 我想用我的文章和我的帐户做crud测试。[https://demo.productionready.io/#/] 现在,我已经在config/headers中插入了一个jwt令牌,使用axios导入 请告诉我如何从conductor.productionready.io获取我的列表Javascript 如何在conductor.productionready.io(假api)中获取我的帖子,javascript,reactjs,typescript,api,axios,Javascript,Reactjs,Typescript,Api,Axios,我正在使用由提供的文章api进行crud测试[https://conduit.productionready.io/api/articles] 我想用我的文章和我的帐户做crud测试。[https://demo.productionready.io/#/] 现在,我已经在config/headers中插入了一个jwt令牌,使用axios导入 请告诉我如何从conductor.productionready.io获取我的列表 async function fetchPosts(func: any)
async function fetchPosts(func: any){
await axios.get('https://conduit.productionready.io/api/articles', {
headers: {
authorization: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MTQwMTA4LCJ1c2VybmFtZSI6InlldW1teSIsImV4cCI6MTYxNzU0NTI1OH0.jn5NLpUP15nV56W19OJpywcaPf9ALJRGVOQ_wC_ept8'
}
})
.then(func)
.catch(console.log);
}
const loadPosts = ((posts: Post[], func: any)=>{
if (posts.length === 0) fetchPosts(func);
})
export default function PostListView() {
const initState: Post[] = [];
const [posts, setPosts] = useState(initState);
if (!posts) return <div> Loading... </div>;
// eslint-disable-next-line react-hooks/rules-of-hooks
useEffect(()=>{
loadPosts(posts,
(response: AxiosResponse)=>{
setPosts(response.data.articles);
}
)
}, [])
// @ts-ignore
return (
<div
id="PostListView"
style={{display: 'flex', flexDirection: 'column', justifyContent: 'center', alignItems: 'center'}}>
{posts.map(post => <PostCard key={post.createdAt} post={post} /> )}
</div>
);
}
异步函数fetchPosts(func:any){
等待axios。获取更多信息https://conduit.productionready.io/api/articles', {
标题:{
授权:“EYJ0EXAIIOIJKV1QILCJHBGCIOIJIUZI1NIJ9.EYJPZCI6MTQMTA4LCJ1C2VYBMFTZSI6INLLDW1TESIMV4CCI6MTYXNZU0NTI1OH0.JN5NLPUP15NV56W19OJPYWCAPF9ALGVOQ_wC_ept8”
}
})
.然后(func)
.catch(console.log);
}
const loadPosts=((posts:Post[],func:any)=>{
如果(posts.length==0)fetchPosts(func);
})
导出默认函数PostListView(){
常量initState:Post[]=[];
const[posts,setPosts]=useState(initState);
如果(!posts)返回加载;
//eslint禁用下一行反应挂钩/挂钩规则
useffect(()=>{
装货柱,
(响应:AxiosResponse)=>{
设置柱(响应、数据、文章);
}
)
}, [])
//@ts忽略
返回(
{posts.map(post=>)}
);
}
您正在使用
等待
和然后()
。您应该只使用其中一个。在您的情况下,我建议删除async
和wait
。感谢您的反馈,我认为我必须同时使用async和promise。。但是你知道如何解决我的问题吗?