【问题标题】:Getting data from cloud firestore using nodejs takes too long使用 nodejs 从 cloud firestore 获取数据需要很长时间
【发布时间】:2019-12-07 20:57:40
【问题描述】:

我正在使用 NodeJs 应用程序测试 firebase,当我尝试从使用一个集合创建的数据库中读取数据时,它会获取数据,但在我的终端上记录数据需要太多时间。

const express = require('express');
const bodyParser = require('body-parser');
const admin = require('firebase-admin');

let serviceAccount = require('./ServiceAccountKey.json');

const app = express();
const port = 3000;

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://database url"
});

let db = admin.firestore();

db.collection('messages').get()
  .then((snapshot) => {
    snapshot.forEach((doc) => {
      console.log(doc.id, '=>', doc.data());
    });
  })
  .catch((err) => {
    console.log('Error getting documents', err);
  });

我也尝试在数据库上写入数据,但是当我尝试使用以下代码执行此操作时,我总是收到以下错误:

let docRef = db.collection('test').doc('alovelace');

let setAda = docRef.set({
  first: 'Ada',
  last: 'Lovelace',
  born: 1815
}).then(function() {
  console.log('Data saved!');
}).catch(function(error) {
  console.log("Gor an error: ", error);
});

我想了解我做错了什么,因为我只阅读 1 项并尝试仅插入一项。非常感谢您的支持。

【问题讨论】:

  • 使用snapshot.forEach 处理该集合中没有文档的情况是一种很好的做法。这可以像在上面的行上使用if (snapshot.empty) { throw 'expected documents' } 一样简单。
  • 我花了一些时间试图重现该问题,但无济于事。您提供的代码可以正常工作(在启动节点进程的 1-2 秒内响应),因此您的网络在这里可能存在问题。抱歉,我无法提供更多帮助。
  • @samthecodingman 感谢您的插图和测试我的代码的努力。我真的很困惑,因为阅读方法有效,但写作方法却没有。我认为我使用 Firestore 的方式有问题。我将尝试其他连接,看看它是否按预期工作。

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


【解决方案1】:

可能存在一些过时的依赖关系。尝试使用

npm update

【讨论】:

    猜你喜欢
    • 2020-05-02
    • 1970-01-01
    • 2021-02-24
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多