Home > Blockchain >  failed to read stream, no exist StreamNotFoundError: stream not found
failed to read stream, no exist StreamNotFoundError: stream not found

Time:05-22

As per documentation:

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

under: Checking if the stream exists

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;
}

i do get:

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

Their tutorials seems outdated for me.

Q: Is there a way to check if a stream exist ( and maybe add if not?) in event store in 2022?

Edit 2:

after the attempts, it still shows with the provided answer: node:events:505 throw er; // Unhandled 'error' event ^

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

CodePudding user response:

I am not sure what you have in your database, but I quickly copied the samples code and it works as expected

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"));

When I run it I get:

Stream not found
done

CodePudding user response:

The issue here seems to be that you're trying to check if the stream exists after it's already finished being called.

It may be helpful to move the client.readStream() inside the try/catch, await that directly, then loop over as needed. This at least narrows the point at which it's going wrong, and fails before the for loop is begun.

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) {...}

  • Related