Javascript 使用.then inside if语句解析JSON

Javascript 使用.then inside if语句解析JSON,javascript,Javascript,我正在发出这个api请求,检查是在if语句中执行的,如何在不执行另一个请求的情况下包含then json() database = () => { const { currentUser } = fire.auth(); console.log('loading ...'); var url = 'https://api.com./' + currentUser.email; fetch(url) .then(response => {

我正在发出这个api请求,检查是在if语句中执行的,如何在不执行另一个请求的情况下包含then json()

  database = () => {
    const { currentUser } = fire.auth();
    console.log('loading ...');
    var url = 'https://api.com./' + currentUser.email;
    fetch(url)
      .then(response => {
        if (response.status === 404) {
          console.log(response.status);
          console.log('denied');
        }
        else if (response.status === 200) {
          fetch(url)
            .then(promise => promise.json())
            .then(data => {
              console.log(data)
            })
        }
        else {
          console.log('?')
        }
      });
  }
我尝试使用
response.json()
,但这件事被包装在承诺中,无法访问格式化数据

未处理的拒绝(TypeError):response.then不是函数


请建议更好的选择是
抛出
错误,而不是记录错误,从而打破
然后
链:

url='1〕https://jsonplaceholder.typicode.com/todos/1';
获取(url)
。然后(响应=>{
如果(response.status==404)
抛出新错误(“未找到”);
如果(response.status!==200)
抛出新错误(“错误”);
返回response.json()
})
.then(data=>console.log('ok',data))

.catch(err=>console.log('error',err.message))更好的选择是
抛出错误,而不是记录错误,从而打破
然后
链:

url='1〕https://jsonplaceholder.typicode.com/todos/1';
获取(url)
。然后(响应=>{
如果(response.status==404)
抛出新错误(“未找到”);
如果(response.status!==200)
抛出新错误(“错误”);
返回response.json()
})
.then(data=>console.log('ok',data))

.catch(err=>console.log('error',err.message))也许您可以尝试使用
async
-
wait
。使您的代码更简洁

const database = async() => {
    console.log('loading ...');
    var url = 'http://dummy.restapiexample.com/api/v1/employees';
    let response = await fetch(url)
        if (response.status === 404) {
          console.log(response.status);
          console.log('denied');
        } 
        else if (response.status === 200) {
          const data = await response.json()
          console.log(data)
        }
  }

也许您可以尝试使用
async
-
wait
。使您的代码更简洁

const database = async() => {
    console.log('loading ...');
    var url = 'http://dummy.restapiexample.com/api/v1/employees';
    let response = await fetch(url)
        if (response.status === 404) {
          console.log(response.status);
          console.log('denied');
        } 
        else if (response.status === 200) {
          const data = await response.json()
          console.log(data)
        }
  }

代码中的
response.json()
在哪里?您确定错误正确吗?您没有使用
响应。然后
任何地方。为什么
获取(url)
在else if中再次出现?
response.then
在代码中的位置是什么?为什么有两个fetch(url)-我相信它应该是responseBoth response.json()和response.then()的一个fetch调用。如果代码中没有response.json()和response.then(),请确保粘贴了正确的代码&如果您使用的是response.then,那么您的做法是错误的。您已经在使用then on fetch调用,它将为您提供服务器返回的实际响应,而不是promise instanceWhere is
response.json()
,在您的代码中?您确定错误正确吗?您没有使用
响应。然后
任何地方。为什么
获取(url)
在else if中再次出现?
response.then
在代码中的位置是什么?为什么有两个fetch(url)-我相信它应该是responseBoth response.json()和response.then()的一个fetch调用。如果代码中没有response.json()和response.then(),请确保粘贴了正确的代码&如果您使用的是response.then,那么您的做法是错误的。您已经在使用then on fetch调用,它将为您提供服务器返回的实际响应,而不是promise instanceIt works我实现了一个类似的逻辑,但我正在尝试消除parse JSON错误,console.log(data)始终在404上执行。它工作时我实现了一个类似的逻辑,但我正在尝试消除parse JSON错误,即使在404上,console.log(数据)也总是被执行