failed to read stream, no exist StreamNotFoundError: stream not found

failed to read stream, no exist StreamNotFoundError: stream not found

根据文档:

https://developers.eventstore.com/clients/grpc/reading-events.html#reading-backwards-1

下:检查流是否存在

const events = client.readStream("user", {
  direction: FORWARDS,
  fromRevision: BigInt(10),
  maxCount: 20,
});

try {
  for await (const resolvedEvent of events) {
    console.log(resolvedEvent.event?.data);
  }
} catch (error) {
  if (error instanceof StreamNotFoundError) {
    return;
  }

  throw error;
}

我确实得到了:

failed to read stream, no exist StreamNotFoundError: user not found

他们的教程对我来说似乎已经过时了。

问:有没有办法检查 2022 年事件存储中是否存在流(如果不存在,则可能添加?)?

编辑 2:

在尝试之后,它仍然显示提供的答案: node:events:505 扔呃; // 未处理的 'error' 事件 ^

StreamNotFoundError: user not found
    at ReadStream._transform (game\node_modules\@eventstore\db-client\dist\streams\utils\ReadStream.js:50:32)

这里的问题似乎是您正在尝试检查流是否存在它已经完成调用之后。

client.readStream() 移到 try/catch 中可能会有所帮助,直接等待,然后根据需要循环。这至少缩小了出错的范围,并且在 for 循环开始之前就失败了。

try {
// Moved down into try block!
  const events = await client.readStream("user", {
    direction: FORWARDS,
    fromRevision: BigInt(10),
    maxCount: 20,
  });

  for (const resolvedEvent of events) {
    console.log(resolvedEvent.event?.data);
  }
  ...
} catch (error) {...}

我不确定你的数据库中有什么,但我很快复制了示例代码并且它按预期工作

const {EventStoreDBClient, FORWARDS, StreamNotFoundError, START} = require("@eventstore/db-client");

async function main() {
    const CLOUD_ID = "ca1loplo0aepjgt215o0";
    const client   = EventStoreDBClient.connectionString`esdb+discover://${CLOUD_ID}.mesdb.eventstore.cloud`;

    const events = client.readStream("doesntexist", {
        direction:    FORWARDS,
        fromRevision: START,
        maxCount:     20,
    });

    try {
        for await (const resolvedEvent of events) {
            console.log(resolvedEvent.event?.data);
        }
    } catch (error) {
        if (error instanceof StreamNotFoundError) {
            console.log("Stream not found");
            return;
        }

        throw error;
    }
}

main().then(() => console.log("done"));

当我 运行 它时,我得到:

Stream not found
done