【问题标题】:Console log a specific object attribute from Firestore in Ionic控制台在 Ionic 中记录来自 Firestore 的特定对象属性
【发布时间】:2018-09-26 17:26:32
【问题描述】:

我有一个查询值,我从查询中迭代每个项目并将它们字符串化

if (item.exists) {
auxint++;
auxString += '{"id":"' + item.id + '","AgendaTeste":' + JSON.stringify(item.data()) + '}';

}`

我正在使用文档 ID 在文档中的所有内容中循环。现在,从这一点来看,我的问题出在哪里。我需要做的是控制台记录我的对象中的一个特定属性,我称之为“Documento”。

这是我的对象结构:

现在我想控制日志并将其值存储在某处的特定属性是“Hora”属性。

我可以控制台记录 item.id,但我不能控制台记录 item.id.Documento.Hora 或 item.id.Hora,这就是我需要做的。

这是我的完整代码:

export class ConsultasPage {
public myTime = new Date();
public dataJSON;
public dataAux;
paciente: string = "Genésio Euclides"
public medico: any = "Benedito";
ubs: any;
private db: any;
model: any = {};
isEditing: boolean = false;


constructor(public navCtrl: NavController, public modalCtrl: ModalController) {
    var query = firebase.firestore().collection("AgendaTeste")
    var auxint = 0;
    this.dataAux
    let auxString = '[';
    //db.collection('Agenda').where("Documento.id", "==", '1').get().then(res => {
    query.where('Documento.Paciente', '==', this.paciente).get().then(res => {
        res.forEach(item => {
            if (item.exists) {
                auxint++;
                auxString += '{"id":"' + item.id + '","AgendaTeste":' + JSON.stringify(item.data()) + '}';
                console.log(item.);

            }
            if (res.size != auxint)
                auxString += ', ';
        })
        auxString += ']';
        this.dataJSON = JSON.parse(auxString);
        console.log(this.dataJSON.DataCriacao);
    }).catch(err => {
        console.log('Ocorreu um erro ' + err);
    });

}

}

【问题讨论】:

    标签: json angular ionic-framework google-cloud-firestore


    【解决方案1】:

    这个问题的解决办法是这样的:

    this.mostraHora = item.get("Documento.Hora");
    console.log(this.mostraHora);
    

    在这一行:

    res.forEach(item => {
    

    item 代表 firebase.firestore.QueryDocumentSnapshot。

    现在根据文档“可以使用 .data() 或 .get() 提取数据以获取特定字段”此链接有更多解释:https://firebase.google.com/docs/reference/js/firebase.firestore.DocumentSnapshot#data

    现在我们这里基本上是 firebase.firestore.QueryDocumentSnapshot.get(FieldPath) 或 FieldValue

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 2014-08-17
      • 2020-07-30
      • 1970-01-01
      • 2021-04-13
      相关资源
      最近更新 更多