【问题标题】:I want to update the data without refreshing the page when there is some changes in Firestore当 Firestore 发生一些变化时,我想在不刷新页面的情况下更新数据
【发布时间】:2023-03-27 01:05:02
【问题描述】:

我正在尝试使用 Firestore 创建 REST,我的目标是在我的数据库发生更改时不断更新结果而不刷新页面。

const express = require('express')
const app = express()


const admin = require('firebase-admin')
const servicAccount = require('./firebase-adminsdk.json')
admin.initializeApp({
    credential: admin.credential.cert(servicAccount)
})
const db = admin.firestore()
app.get('/', (req, res) => {
    // SSE Setup
    res.writeHead(200, {
        'Content-Type': 'text/event-stream',
        'Cache-Control': 'no-cache',
        'Connection': 'keep-alive',
    });
    const doc = db.collection('TestingData').doc('uQZu5OA0XZZQtG0fKSVG');
    const observer = doc.onSnapshot(doc => res.write(doc.data()))
  });
  app.listen(3000)
    

错误:

_http_outgoing.js:696
    throw new ERR_INVALID_ARG_TYPE('first argument',
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of Object
    at write_ (_http_outgoing.js:696:11)
    at ServerResponse.write (_http_outgoing.js:661:15)
    at C:\Users\Lmaoooooooo\Desktop\Testing Firebase\index.js:19:48
    at DocumentWatch.onNext (C:\Users\Lmaoooooooo\Desktop\Testing Firebase\node_modules\@google-cloud\firestore\build\src\reference.js:417:21)
    at DocumentWatch.pushSnapshot (C:\Users\Lmaoooooooo\Desktop\Testing Firebase\node_modules\@google-cloud\firestore\build\src\watch.js:449:18)
    at DocumentWatch.onData (C:\Users\Lmaoooooooo\Desktop\Testing Firebase\node_modules\@google-cloud\firestore\build\src\watch.js:333:26)
    at PassThrough.<anonymous> (C:\Users\Lmaoooooooo\Desktop\Testing Firebase\node_modules\@google-cloud\firestore\build\src\watch.js:296:26)
    at PassThrough.emit (events.js:315:20)
    at addChunk (_stream_readable.js:309:12)
    at readableAddChunk (_stream_readable.js:284:9) {
  code: 'ERR_INVALID_ARG_TYPE'
}

【问题讨论】:

  • 这是一个非常不具体和广泛的问题。是的,nodejs 可以与任何具有您可以从 Javascript 或某些网络协议访问的 API 的数据库一起使用。不知道你还问什么。如果您对 Firestore 有特定问题,请提出特定问题并显示您尝试使用的代码。 “有什么方法可以实现”对于任何人来说都不是一个足够具体的问题来回答。我们不知道你想做什么。我们不知道您尝试了什么代码?我们不知道您遇到了什么问题?
  • @jfriend00 抱歉没有具体说明,我已经编辑了我的问题并提供了代码和目标。希望这次能帮上忙。谢谢。
  • 您得到的错误非常具有描述性和具体性。 res.write() 只接受某些参数类型,而您没有给它一种允许的类型。您正在向它传递一个对象。做不到。

标签: node.js rest firebase-realtime-database


【解决方案1】:

您收到的错误非常具有描述性和具体性。 res.write() 只接受某些参数类型,而您没有给它一种允许的类型。您正在向它传递一个不允许的对象。

这是因为doc.data() 返回一个对象。如果您希望将对象作为响应发送,那么您可以更改内容以将其作为 JSON 发送,如下所示:

app.get('/', (req, res) => {
    res.writeHead(200, {
        'Content-Type': 'application/json',        // set JSON content-type
        'Cache-Control': 'no-cache',
        'Connection': 'keep-alive',
    });
    const doc = db.collection('TestingData').doc('uQZu5OA0XZZQtG0fKSVG');
    const observer = doc.onSnapshot(doc => res.write(JSON.stringify(doc.data())));
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-08
    • 2017-05-11
    • 2011-06-15
    相关资源
    最近更新 更多