根据先前请求的条件发送请求
Send request depending on a condition from a previous request
我想根据上一个请求的条件发送请求。
这是我的代码:
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))
}
它有效,但我是编程新手,我不确定这是否是 'best' 的方法。
我的意思是我根本不知道这是否是处理此问题的好方法。
在此先致谢,祝您有愉快的一天!
你写的代码很有道理,不用太担心。如果您熟悉 async await 语法,则应将其更改为该语法。它只是更具可读性。这是一个布局:
const sendFriendRequest_ver_2 = (requestFrom, requestTo, requestFromUserType = "INDIVIDUAL_USER", requestToUserType = "INDIVIDUAL_USER") => {
const data = {
requestFrom,
requestTo,
requestFromUserType,
requestToUserType,
}
(async()=>{
try{
const res = await api.get(`/api/friendship-request?userId=${requestTo}userType=INDIVIDUAL_USER`)
const found = res.data.some(item => item.id === requestFrom);
if (found) {
console.log('invite already sent')
} else {
console.log('sending now')
const res = await api.post('/api/friendship-request', data)
}
}
catch(error){
console.log(e)
}
})()
我想根据上一个请求的条件发送请求。
这是我的代码:
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))
}
它有效,但我是编程新手,我不确定这是否是 'best' 的方法。
我的意思是我根本不知道这是否是处理此问题的好方法。
在此先致谢,祝您有愉快的一天!
你写的代码很有道理,不用太担心。如果您熟悉 async await 语法,则应将其更改为该语法。它只是更具可读性。这是一个布局:
const sendFriendRequest_ver_2 = (requestFrom, requestTo, requestFromUserType = "INDIVIDUAL_USER", requestToUserType = "INDIVIDUAL_USER") => {
const data = {
requestFrom,
requestTo,
requestFromUserType,
requestToUserType,
}
(async()=>{
try{
const res = await api.get(`/api/friendship-request?userId=${requestTo}userType=INDIVIDUAL_USER`)
const found = res.data.some(item => item.id === requestFrom);
if (found) {
console.log('invite already sent')
} else {
console.log('sending now')
const res = await api.post('/api/friendship-request', data)
}
}
catch(error){
console.log(e)
}
})()