根據檔案:
https://developers.eventstore.com/clients/grpc/reading-events.html#reading-backwards-1
under:檢查流是否存在
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 throw er; // 未處理的“錯誤”事件 ^
StreamNotFoundError: user not found
at ReadStream._transform (game\node_modules\@eventstore\db-client\dist\streams\utils\ReadStream.js:50:32)
uj5u.com熱心網友回復:
我不確定您的資料庫中有什么,但我很快復制了示例代碼,它按預期作業
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
uj5u.com熱心網友回復:
這里的問題似乎是您正在嘗試在已完成呼叫后檢查流是否存在。
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) {...}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/479739.html
標籤:javascript 数据库 事件存储数据库
下一篇:tkinter中的文本
