Handle non-insert changes

This commit is contained in:
Erki Aas 2022-10-11 02:24:41 +03:00
parent 4b8591f51e
commit 26e79bbb20

View File

@ -33,7 +33,6 @@ async function run() {
console.log("Started watching changes in database"); console.log("Started watching changes in database");
const writeMessage = (response, blob) => { const writeMessage = (response, blob) => {
// TODO: why no id?
const id = blob._id || null const id = blob._id || null
const message = `id: ${id}\nevent: message\ndata: ${JSON.stringify(blob)}\n\n` const message = `id: ${id}\nevent: message\ndata: ${JSON.stringify(blob)}\n\n`
response.write(message) response.write(message)
@ -55,7 +54,10 @@ async function run() {
}); });
const changeListener = async (change) => { const changeListener = async (change) => {
writeMessage(response, change.fullDocument) // Ignore events without fullDocument, e.g. deletes.
if (change.fullDocument) {
writeMessage(response, change.fullDocument)
}
} }
changeStream.on("change", changeListener); changeStream.on("change", changeListener);
response.on('close', () => { response.on('close', () => {