【问题标题】:getting value from cloud firestore从 Cloud Firestore 中获取价值
【发布时间】:2018-12-06 07:49:52
【问题描述】:

我正在使用以下代码从 Cloud Firestore 获得价值

dbManager = admin.initializeApp(functions.config().firebase);

this.dbManager.collection("test").doc("myTest").get().then( async test=> {
    console.log("test---------------->",test);
    console.log("test---------------->",test._fieldsProto['tests']);
    console.log("test---data------------->",test.data()['tests'].values);
});

而我使用test.data()['tests']时得到的值是

{ values: 
   [ { mapValue: [Object], valueType: 'mapValue' },
     { mapValue: [Object], valueType: 'mapValue' },
     { mapValue: [Object], valueType: 'mapValue' },
     { mapValue: [Object], valueType: 'mapValue' },
     { mapValue: [Object], valueType: 'mapValue' },
     { mapValue: [Object], valueType: 'mapValue' },
     { mapValue: [Object], valueType: 'mapValue' },
     { mapValue: [Object], valueType: 'mapValue' },
     { mapValue: [Object], valueType: 'mapValue' } ] }

如何从中获取值?当我尝试使用 test.data()['tests'].array.forEach 之类的 foreach 时,它不起作用。

【问题讨论】:

  • 我无法弄清楚您的实际文档是什么样的。您能否发布myTest 文档的屏幕截图,如Firebase Database console 中所示?
  • 以什么方式“不工作”?你得到错误的输出,或者根本没有,或者一个错误?

标签: node.js typescript firebase google-cloud-firestore


【解决方案1】:

test.data()['tests'] 不返回数组,而是返回映射 {values: []}

你需要做的是先获取values,然后遍历数组:

test.data()['tests'].values.forEach(x => console.log(x));

console.log() 替换为您想要对所有条目执行的操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 2019-09-30
    • 2020-03-17
    • 1970-01-01
    相关资源
    最近更新 更多