Node.js 当Firestore中有一些更改时,我希望在不刷新页面的情况下更新数据

Node.js 当Firestore中有一些更改时,我希望在不刷新页面的情况下更新数据,node.js,rest,firebase-realtime-database,Node.js,Rest,Firebase Realtime Database,我正在尝试使用Firestore创建REST,我的目标是在数据库发生更改时不刷新页面而不断更新结果 const express = require('express') const app = express() const admin = require('firebase-admin') const servicAccount = require('./firebase-adminsdk.json') admin.initializeApp({ credential: admin

我正在尝试使用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'
}
\u http\u outgoing.js:696
抛出新错误\u无效的\u参数\u类型('第一个参数',
^
TypeError[ERR_INVALID_ARG_TYPE]:第一个参数的类型必须为string或Buffer或Uint8Array的实例。收到了Object的实例
在写入时(_http_outgoing.js:696:11)
在ServerResponse.write(_http_outgoing.js:661:15)
在C:\Users\lmaooo\Desktop\Testing Firebase\index.js:19:48
在DocumentWatch.onNext(C:\Users\lmaoooo\Desktop\Testing Firebase\node\u modules\@google cloud\firestore\build\src\reference.js:417:21)
在DocumentWatch.pushSnapshot(C:\Users\lmaoooo\Desktop\Testing Firebase\node\u modules\@google cloud\firestore\build\src\watch.js:449:18)
在DocumentWatch.onData(C:\Users\lmaoooo\Desktop\Testing Firebase\node\u modules\@google cloud\firestore\build\src\watch.js:333:26)
通过时。(C:\Users\lmaoooo\Desktop\Testing Firebase\node\u modules\@google cloud\firestore\build\src\watch.js:296:26)
在PassThrough.emit(events.js:315:20)
在addChunk(_stream_readable.js:309:12)
在readableAddChunk(_stream_readable.js:284:9){
代码:“错误\无效\参数\类型”
}

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

这是因为
doc.data()

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())));
});

这是一个非常不具体且宽泛的问题。是的,nodejs可以与任何具有可以通过Javascript或某些网络协议访问的API的数据库一起工作。不确定您还问了什么。如果您对Firestore有特定的问题,请问一个特定的问题并显示您尝试使用的代码。"有没有办法落实?"不是一个任何人都能回答的足够具体的问题。我们不知道你在尝试做什么。我们不知道你尝试了什么代码?我们不知道你遇到了什么问题?@jfriend00抱歉,我已经编辑了我的问题,并提供了代码和目标。希望你这次能提供帮助。谢谢。你收到的错误非常严重说明性和特定。
res.write()
只接受某些参数类型,而您没有为其提供允许的类型之一。您正在向其传递一个对象。无法这样做。