mongoose mongodb change streams:如何使用TypeScript“change”事件?

uajslkp6  于 12个月前  发布在  Go
关注(0)|答案(1)|浏览(138)

我从mongoose v5升级到v6,现在下面的代码不再工作。ChangeEvent<ListingDocument>不再是更改参数的正确类型。
下面是在mongoose v5上运行的代码:

export default function watchListing() {
    Listing.watch([], {fullDocument: "updateLookup"}).on("change", change => {
        watchUpdatePrices(change);
    });
}

function watchUpdatePrices(change: ChangeEvent<ListingDocument>) {
    ...
});

字符串
现在我得到以下错误:

Argument of type 'ChangeStreamDocument<any>' is not assignable to parameter of type 'ChangeEvent<ListingDocument>'.
  Type 'ChangeStreamDropDocument' is not assignable to type 'ChangeEvent<ListingDocument>'.
    Type 'ChangeStreamDropDocument' is not assignable to type 'ChangeEventOther<ListingDocument>'.
      Types of property '_id' are incompatible.
        Type 'unknown' is not assignable to type 'ResumeToken'.ts(2345)


你是如何改变流的?

w80xi6nr

w80xi6nr1#

在mongodb(v6)中使用ChangeStreamDocument

import { ChangeStreamDocument } from 'mongodb';
export default function watchListing() {
Listing.watch([], {fullDocument: "updateLookup"}.on("change", change:ChangeStreamDocument<ListingDocument> => {
    watchUpdatePrices(change);
});
}
function watchUpdatePrices(change: ChangeStreamDocument<ListingDocument>) {
        ...
    // Check  ListingDocument.operationType to get respective type for update,create,del etc
    if(data.operationType==="update"){
      //then ... 
    }
});

字符串

相关问题