简体   繁体   English

警告可能未处理的 Promise 拒绝 (id: 21): TypeError: undefined is not an object (evaluating 'res.json')

[英]WARN Possible Unhandled Promise Rejection (id: 21): TypeError: undefined is not an object (evaluating 'res.json')

WARN Possible Unhandled Promise Rejection (id: 21): TypeError: undefined is not an object (evaluating 'res.json') How can I fix this problem in my code? WARN Possible Unhandled Promise Rejection (id: 21): TypeError: undefined is not an object (evaluating 'res.json') 如何在我的代码中解决这个问题? I tried logging user and loggeduserobj both logs correctly but still, this error is coming I also logged the type of loggeduserobj it logs object Can you help me to fix this problem?我尝试 logging user 和 loggeduserobj 都正确记录,但仍然出现此错误我还记录了 loggeduserobj 的类型,它记录 object 你能帮我解决这个问题吗?

const { user } = route.params;

  const [issameuser, setIssameuser] = useState(false)
  const [follow,  SetFollow] = useState(false)

  const isMyProfile = async (otherprofile) => {
    AsyncStorage.getItem('user').then((loggeduser) => {
      const loggeduserobj = JSON.parse(loggeduser)
      if (loggeduserobj.user.username == otherprofile[0].username) {
        setIssameuser(true)
      }
      else {
        setIssameuser(false)
      }
    })
  }
 

  const CheckFollow = async (otherprofile) => {

    AsyncStorage.getItem('user')
      .then(loggeduser => {
        const loggeduserobj = JSON.parse(loggeduser);
        fetch('http://10.0.2.2:3000/checkfollow', {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json'
          },
          body: JSON.stringify({
           followfrom : loggeduserobj.user.username, followto: otherprofile[0].username
          })
        })
      })
      .then(res => res.json())
      .then(data => {
        if (data.message == "User in following list") {
          SetFollow(true)
        }
        else if (data.message == "User not in following list") {
          SetFollow(false)
        }
        else {
          alert('Please Try Again!')
        }

      })

  }

  useEffect(() => {
    isMyProfile(user)
    CheckFollow(user)
  }, [])

Backend:后端:

router.post('/checkfollow', (req, res) => {
    const { followfrom, followto } = req.body;
    console.log(followfrom, followto);
    if (!followfrom || !followto) {
        return res.status(422).json({ error: "Invalid Credentials" });
    }
    User.findOne({ username: followfrom })
        .then(mainuser => {
            if (!mainuser) {
                return res.status(422).json({ error: "Invalid Credentials" });
            }
            else {
                let data = mainuser.following.includes(followto);
                console.log(data);
                if (data == true) {
                    res.status(200).send({
                        message: "User in following list"
                    })
                }
                else {
                    res.status(200).send({
                        message: "User not in following list"
                    })
                }
            }

        })
        .catch(err => {
            return res.status(422).json({ error: "Server Error" });
        })
})

You need to return a Promise , the result of fetch , from your first .then() so that it can be chained on, like so:您需要从您的第一个 .then .then()返回一个Promise ,即fetch的结果,以便可以将其链接起来,如下所示:

const CheckFollow = async (otherprofile) => {
  AsyncStorage.getItem('user')
    .then(loggeduser => {
      const loggeduserobj = JSON.parse(loggeduser);
      return fetch('http://10.0.2.2:3000/checkfollow', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({
          followfrom : loggeduserobj.user.username, followto: otherprofile[0].username
        })
      })
    })
    .then(res => res.json())
    .then(data => {
      if (data.message == "User in following list") {
        SetFollow(true)
      } else if (data.message == "User not in following list") {
        SetFollow(false)
      } else {
        alert('Please Try Again!')
      }
    })
}

You should also consider using await in this case, especially since your function is already marked as async to make things more readable您还应该考虑在这种情况下使用await ,特别是因为您的 function 已经标记为async以使内容更具可读性

const CheckFollow = async (otherprofile) => {
  const loggeduser = await AsyncStorage.getItem('user');
  const loggeduserobj = JSON.parse(loggeduser);

  const res = await fetch('http://10.0.2.2:3000/checkfollow', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      followfrom : loggeduserobj.user.username, followto: otherprofile[0].username
    })
  });

  const data = await res.json();

  if (data.message == "User in following list") {
    SetFollow(true)
  } else if (data.message == "User not in following list") {
    SetFollow(false)
  } else {
    alert('Please Try Again!')
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 可能未处理的承诺拒绝(Id:0):TypeError:undefined不是对象(评估'err.response.data') - Possible Unhandled Promise Rejection (Id: 0): TypeError: undefined is not an object (evaluating 'err.response.data') React Native:可能的未处理的承诺拒绝(id:0):TypeError:undefined不是对象 - React Native: Possible Unhandled Promise Rejection (id: 0): TypeError: undefined is not an object 可能的未处理的承诺拒绝(id:0):undefined不是对象 - Possible Unhandled Promise Rejection (id:0): undefined is not an object [未处理的承诺拒绝:类型错误:未定义不是对象(评估'_ref.about')] - [Unhandled promise rejection: TypeError: undefined is not an object (evaluating '_ref.about')] 未处理的 Promise 拒绝:类型错误:未定义不是 object(正在评估“data.date”) - Unhandled Promise Rejection: TypeError: undefined is not an object (evaluating 'data.date') [未处理的承诺拒绝:类型错误:未定义不是对象(评估“response.data”)] - [Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'response.data')] React Native:未处理的promise promise:TypeError:undefined不是对象(评估'response.json') - React Native: Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'response.json') 警告:可能未处理 Promise 拒绝 (id: 0) - Warn : Possible Unhandled Promise Rejection (id: 0) 'UnhandledPromiseRejection'警告:未处理的承诺拒绝(拒绝ID:2):TypeError:res.status(…).json(…).catch不是函数 - 'UnhandledPromiseRejection' Warning: Unhandled promise rejection (rejection id: 2): TypeError: res.status(…).json(…).catch is not a function 可能未处理的承诺拒绝 (id: 0): undefined 不是一个对象 ('prevComponentInstance._currentElement') - Possible Unhandled Promise Rejection (id: 0): undefined is not an object ('prevComponentInstance._currentElement')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM