Handle non-insert changes
This commit is contained in:
parent
4b8591f51e
commit
26e79bbb20
@ -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,8 +54,11 @@ async function run() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const changeListener = async (change) => {
|
const changeListener = async (change) => {
|
||||||
|
// Ignore events without fullDocument, e.g. deletes.
|
||||||
|
if (change.fullDocument) {
|
||||||
writeMessage(response, change.fullDocument)
|
writeMessage(response, change.fullDocument)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
changeStream.on("change", changeListener);
|
changeStream.on("change", changeListener);
|
||||||
response.on('close', () => {
|
response.on('close', () => {
|
||||||
changeStream.removeListener("change", changeListener)
|
changeStream.removeListener("change", changeListener)
|
||||||
|
Loading…
Reference in New Issue
Block a user