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

uajslkp6  于 2024-01-08  发布在  Go
关注(0)|答案(1)|浏览(157)

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

  1. export default function watchListing() {
  2. Listing.watch([], {fullDocument: "updateLookup"}).on("change", change => {
  3. watchUpdatePrices(change);
  4. });
  5. }
  6. function watchUpdatePrices(change: ChangeEvent<ListingDocument>) {
  7. ...
  8. });

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

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


你是如何改变流的?

w80xi6nr

w80xi6nr1#

在mongodb(v6)中使用ChangeStreamDocument

  1. import { ChangeStreamDocument } from 'mongodb';
  2. export default function watchListing() {
  3. Listing.watch([], {fullDocument: "updateLookup"}.on("change", change:ChangeStreamDocument<ListingDocument> => {
  4. watchUpdatePrices(change);
  5. });
  6. }
  7. function watchUpdatePrices(change: ChangeStreamDocument<ListingDocument>) {
  8. ...
  9. // Check ListingDocument.operationType to get respective type for update,create,del etc
  10. if(data.operationType==="update"){
  11. //then ...
  12. }
  13. });

字符串

相关问题