Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将Firebase应用程序代码转换为使用Fetch API_Javascript_Firebase_Firebase Realtime Database_Fetch Api - Fatal编程技术网

Javascript 将Firebase应用程序代码转换为使用Fetch API

Javascript 将Firebase应用程序代码转换为使用Fetch API,javascript,firebase,firebase-realtime-database,fetch-api,Javascript,Firebase,Firebase Realtime Database,Fetch Api,我正在处理此存储库中的代码,并希望将其修改为使用fetch,而不是当前使用的黑客新闻API特定代码(作为学习练习) 代码是否需要firebase/app和firebase/database。在我花费大量时间在这上面之前,我(只有基本的Javascript/React经验)是否可以将其更新为使用fetch而不是firebase专有代码 我的理解是,我可以使用fetch检索数据,即基于以下内容的数据: fetch('https://hacker-news.firebaseio.com/v0/')

我正在处理此存储库中的代码,并希望将其修改为使用
fetch
,而不是当前使用的黑客新闻API特定代码(作为学习练习)

代码是否需要
firebase/app
firebase/database
。在我花费大量时间在这上面之前,我(只有基本的Javascript/React经验)是否可以将其更新为使用
fetch
而不是firebase专有代码

我的理解是,我可以使用fetch检索数据,即基于以下内容的数据:

fetch('https://hacker-news.firebaseio.com/v0/')
  .then(response => {
    return response.json()
  })
  .then(data => {
    // Work with JSON data here
    console.log(data)
  })

  })
我不确定如何模拟这类函数,因为它们使用
firebase.database()
code

function storiesRef(path) {
  return api.child(path)
}

function itemRef(id) {
  return api.child('item/' + id)
}

function userRef(id) {
  return api.child('user/' + id)
}

function updatesRef() {
  return api.child('updates/items')

假设您的数据结构相同,您应该能够以相同的方式访问数据对象;因此,对于
itemRef
函数:
返回数据

function userRef(id) {
   return dataAPI.user[id];
}

var user = userRef('1234567890xxxx');
只要您有从给定端点的api请求返回的数据,您就应该能够像任何典型对象一样对此进行推理

var dataAPI = fetch('https://hacker-news.firebaseio.com/v0.json') // append .json at the end of the url to make a RESTful request
  .then(response => {
    return response.json()
  })
  .then(data => {
    // Work with JSON data here
    return data;
  }, error => {
    console.error('onRejected function called: ' + error.message);
  })


function storiesRef(path) {
  //return api.child(path)
  return dataAPI[path];
}

function itemRef(id) {
  //return api.child('item/' + id)
  return dataAPI.item[id];
}

function userRef(id) {
  //return api.child('user/' + id)
  return dataAPI.user[id];
}

function updatesRef() {
  //return api.child('updates/items')
  return dataAPI.updates.items;
}
一些其他注释:

  • 要查询Firebase DB url,请在url末尾追加
    .json
    (见:和 )
  • Firebase REST API需要令牌身份验证(请参阅: )
  • Firebase REST API旨在直接从 命令行或服务器(至少对于完整CRUD操作)

  • 假设您的数据结构相同,您应该能够以相同的方式访问数据对象;因此,对于
    itemRef
    函数:
    返回数据

    function userRef(id) {
       return dataAPI.user[id];
    }
    
    var user = userRef('1234567890xxxx');
    
    只要您有从给定端点的api请求返回的数据,您就应该能够像任何典型对象一样对此进行推理

    var dataAPI = fetch('https://hacker-news.firebaseio.com/v0.json') // append .json at the end of the url to make a RESTful request
      .then(response => {
        return response.json()
      })
      .then(data => {
        // Work with JSON data here
        return data;
      }, error => {
        console.error('onRejected function called: ' + error.message);
      })
    
    
    function storiesRef(path) {
      //return api.child(path)
      return dataAPI[path];
    }
    
    function itemRef(id) {
      //return api.child('item/' + id)
      return dataAPI.item[id];
    }
    
    function userRef(id) {
      //return api.child('user/' + id)
      return dataAPI.user[id];
    }
    
    function updatesRef() {
      //return api.child('updates/items')
      return dataAPI.updates.items;
    }
    
    一些其他注释:

  • 要查询Firebase DB url,请在url末尾追加
    .json
    (见:和 )
  • Firebase REST API需要令牌身份验证(请参阅: )
  • Firebase REST API旨在直接从 命令行或服务器(至少对于完整CRUD操作)

  • 假设您的数据结构相同,您应该能够以相同的方式访问数据对象;因此,对于
    itemRef
    函数:
    返回数据
    只要您有从给定端点的api请求返回的数据,您就应该能够像任何典型对象一样对此进行推理。顺便说一句,如果您想查询Firebase DB url,您应该在url末尾附加
    .json
    (请参阅:和)。尽管REST API需要令牌身份验证(请参阅:),并且打算直接从命令行或服务器使用(至少对于完整的CRUD操作)。@UncaughtTypeError您可以将建议的代码放入答案中吗?假设您的数据结构相同,您应该能够以相同的方式访问数据对象;因此,对于
    itemRef
    函数:
    返回数据
    只要您有从给定端点的api请求返回的数据,您就应该能够像任何典型对象一样对此进行推理。顺便说一句,如果您想查询Firebase DB url,您应该在url末尾附加
    .json
    (请参阅:和)。尽管REST API需要令牌身份验证(请参阅:),并且打算直接从命令行或服务器使用(至少对于完整的CRUD操作)。@UncaughtTypeError您可以将建议的代码放在答案中吗?对于
    ,我得到
    {“error”:“权限被拒绝”}
    。请求需要类似于
    `当前代码使用firebase api,如果可能的话,我希望使用fetch,但要使其正常工作,这似乎是一项非常重要的任务。谢谢。我得到了
    {“错误”:“权限被拒绝”}
    。请求需要类似于
    `当前代码使用firebase api,如果可能的话,我希望使用fetch,但要使其正常工作,这似乎是一项非常重要的任务。谢谢