Google Firebase和http发布到列表 [英] Google Firebase and http Post to Lists

查看:48
本文介绍了Google Firebase和http发布到列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Google Firebase上安装了JSON数据库,并且有一个Angular 6应用程序正在寻找将对象发布到该数据库的方法.一切正常,除了我的列表项中附加了字母数字名称,而且我无法删除它.

I have a JSON database setup on Google Firebase and I have an Angular 6 App that is looking to post an object to that database. Everything works fine except there is an alphanumeric name appended to my list item and I can't get it off.

数据库如下:

{
  "thursdayPanels": {
  "panel": [
    {
      "tagline": "tagline 1",
      "content": "some content 1",
      "id": 1
    },
    {
      "tagline": "tagline 2",
      "content": "some content 2",
      "id": 2
    },
    {
      "tagline": "tagline 3",
      "content": "some content 3"
      "id": 3
    }
  ]
}
}

更新功能如下:

 addPanel(panelInfo){
    return this.http.post(`${this.baseURL}/thursdayCampaign/panel.json`, panelInfo).subscribe((po:Response) => {console.log("po",po)})

其中面板信息是一个具有以下结构的对象,该对象将传递给addPanel()函数:

where panel info is an object structured like this, that gets passed to the addPanel() function:

    this.panelInfo ={
      "week": panel.id,
      "tagline": panel.tagline,
      "content": panel.content,
    }

当面板上载到Firebase时,它看起来像这样(请不要介意名称的不同):我找不到任何Firebase数据库文档,该文档可以告诉我为什么这样做或如何阻止这样做.这会阻止将数据传递给我的* ngFor函数正常工作.

When the panel gets uploaded to firebase it looks like this (please dont mind the name differences): I couldn't find any firebase database documentation that would tell me why it is doing this or how to stop it from doing that. It's preventing the *ngFor functions I pass this data to from working my app.

谢谢.

推荐答案

根据当您执行 POST时,用于实时数据库的REST API文档,以与JavaScript PUT 放在您网址的路径中.

According to the REST API documentation for Realtime Database, when you do a POST, that "pushes" data into the database in the same way that the JavaScript push() method works. It always generates a random ID and adds the data under it. If you don't want this behavior, then you should instead construct the full path of the node to add, and PUT that in the path of your URL.

如果您希望POST将自动在列表中找到下一个可用号码,则不可能.实际上,这根本不是Firebase列表的工作方式.管理数据列表的正确方法是使用 push(),接受随机生成的ID.

If you were hoping that the POST was going to automatically find the next available number in the list, that's not possible. That's actually not how Firebase lists work at all. The proper way to manage a lists of data is with push(), accepting the random generated IDs.

顺序数字索引不是在Realtime Database中管理数据的可扩展方式.您可能想详细了解为什么数组是邪恶的,然后采用另一种方式对数据建模.

Sequential numeric indexes are not a scalable way to manage data in Realtime Database. You might want to read more about why arrays are evil in Firebase, and adopt a different way of modeling your data.

这篇关于Google Firebase和http发布到列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆