根据先前请求的条件发送请求

发布于 2025-02-01 09:29:42 字数 871 浏览 2 评论 0原文

我想根据先前请求的条件发送请求。

这是我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

影子的影子 2025-02-08 09:29:42

您编写的代码很有意义,您不必为此担心太多。如果您熟悉异步等待语法,则应将其更改为此。它更可读。这是一个布局:

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)
 }
 })()

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:

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)
 }
 })()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文