根据先前请求的条件发送请求
我想根据先前请求的条件发送请求。
这是我的代码:
const sendFriendRequest_ver_2 = (requestFrom, requestTo, requestFromUserType = "INDIVIDUAL_USER", requestToUserType = "INDIVIDUAL_USER") => {
const data = {
requestFrom,
requestTo,
requestFromUserType,
requestToUserType,
}
api.get(`/api/friendship-request?userId=${requestTo}&userType=INDIVIDUAL_USER`)
.then(res => {
const found = res.data.some(item => item.id === requestFrom);
if (found) {
console.log('invite already sent')
} else {
console.log('sending now')
api.post('/api/friendship-request', data).then(res => {
}).catch(e => console.log(e))
}
})
.catch(e => console.log(e))
}
它有效,但我是对编程的新手,我不确定这是否是“最佳”方法。
我的意思是我不知道这是否是解决这个问题的好方法。
预先感谢,祝您有美好的一天!
I want to send request depending on a condition from a previous request.
This is my code:
const sendFriendRequest_ver_2 = (requestFrom, requestTo, requestFromUserType = "INDIVIDUAL_USER", requestToUserType = "INDIVIDUAL_USER") => {
const data = {
requestFrom,
requestTo,
requestFromUserType,
requestToUserType,
}
api.get(`/api/friendship-request?userId=${requestTo}&userType=INDIVIDUAL_USER`)
.then(res => {
const found = res.data.some(item => item.id === requestFrom);
if (found) {
console.log('invite already sent')
} else {
console.log('sending now')
api.post('/api/friendship-request', data).then(res => {
}).catch(e => console.log(e))
}
})
.catch(e => console.log(e))
}
It works but I am new to programing and I am not sure if this is the 'best' way to do this.
I mean I dont't know if this is good way to handle this at all.
Thanks in advance and have a nice day!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您编写的代码很有意义,您不必为此担心太多。如果您熟悉异步等待语法,则应将其更改为此。它更可读。这是一个布局:
The code you have written makes sense and you don't need to worry much about it. If you are familiar with async await syntax you should change it to that. its just more readable. Here is a layout: