【问题标题】:My android app shows [object Promise] using Nativescript我的 android 应用程序使用 Nativescript 显示 [object Promise]
【发布时间】:2019-06-16 04:26:59
【问题描述】:

我无法在我的应用上显示我的 Firebase 中的数据。我对此真的很陌生,我不知道该怎么做。我在互联网上找不到任何解决方案。

这是我的打字稿代码:

import { Component, OnInit } from "@angular/core";
import { ActivatedRoute, ActivatedRouteSnapshot } from "@angular/router";

import { Item } from "./item";
import { ItemService } from "./item.service";
import { EventData } from "tns-core-modules/data/observable";
import { Label } from "tns-core-modules/ui/label";
import { listener } from "@angular/core/src/render3";


const firebase = require("nativescript-plugin-firebase");
const firebaseWebApi = require("nativescript-plugin-firebase/app");

@Component({
    selector: "ns-details",
    moduleId: module.id,
    templateUrl: "./item-detail.component.html",
})
export class ItemDetailComponent implements OnInit {

item: Item;
public oneway;

public stuff = firebaseWebApi.database().ref("/WaterStatus")
        .once("value")
        .then(result => console.log(JSON.stringify(result)))
        .catch(error => console.log("Error: " + error));   

constructor(
    private itemService: ItemService,
    private route: ActivatedRoute
) { }

ngOnInit(): void {
    const id = +this.route.snapshot.params["id"];
    this.item = this.itemService.getItem(id);

    var onChildEvent = function(oneway) {
        console.log("Water Status: " + JSON.stringify(oneway.value));
      };

      // listen to changes in the /users path
      firebase.addChildEventListener(onChildEvent, "/WaterStatus").then(
        function(listenerWrapper) {
          var path = listenerWrapper.path;
          var listeners = listenerWrapper.listeners;
        }
      );





   firebase.getValue('/WaterStatus')
      .then(result => console.log(JSON.stringify(result)))
      .catch(error => console.log("Error: " + error));
}

}

这是我的html:

<ActionBar title="Details" class="action-bar"></ActionBar>
<FlexboxLayout flexDirection="column" class="page">
    <FlexboxLayout class="m-15">
        <Label class="h2" [text]="item.id + '. '"></Label>
        <Label class="h2" [text]="item.name"></Label>


    </FlexboxLayout>
    <Label class="h3" [text]="item.role"></Label>
    <!-- <Button text="Tap Me!" tap="onTap" class="btn btn-primary btn-active"></Button> -->
    <br>
    <br>
    <Label class="h3 p-15" text='{{oneway}}' textWrap="true">{{oneway}}</Label>
    <Label class="h3 p-15" text='{{stuff}}' textWrap="true"></Label>
    <Label class="h3 p-15" text='{{last}}' textWrap="true">{{last}}</Label>
</FlexboxLayout>

如何在我的应用中显示我的数据?

有人说我应该做 Promise resolve,但我真的不知道如何将它与它集成。

截图:

screenchot

【问题讨论】:

  • 你在哪里看到[object Promise]?你有这方面的 Playground 样本吗?
  • 我不认为我有它的游乐场样本。我还在赚取 nativescript,这是我第一次处理这样的事情。
  • 我会用截图更新我的帖子

标签: firebase nativescript


【解决方案1】:

查看您的代码,[object Promise] 来自 &lt;Label class="h3 p-15" text='{{stuff}}' textWrap="true"&gt;&lt;/Label&gt;(请通过删除其他表格或为此标签添加不同颜色来确认)。

事实上,stuff 被初始化为一个承诺。相反,您必须删除该初始化,并在 ngOnInit() 中执行以下操作:

firebaseWebApi.database().ref("/WaterStatus")
    .once("value")
    .then(result => {
         console.log(JSON.stringify(result)));
         this.stuff = result; // or result.something?
    })
    .catch(error => console.log("Error: " + error));   

我不是 Firebase 专家,但希望对您有所帮助。

【讨论】:

  • 非常感谢您的回复。这很有帮助。 ?
猜你喜欢
  • 2019-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-14
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
相关资源
最近更新 更多