Eventstore UnknownError: Could not recognize BadRequest

Viewed 71

I'm currently using Eventstore, and I'm receiving the following error: Could not recognize BadRequest;

from:

game process tick failed UnknownError: Could not recognize BadRequest
    at unpackToCommandError (\node_modules\@eventstore\db-client\dist\streams\appendToStream\unpackError.js:53:12)
    at ClientDuplexStreamImpl.<anonymous> (\node_modules\@eventstore\db-client\dist\streams\appendToStream\batchAppend.js:38:66)

For some reason, it seems like it is something with my handler:

async function   gameProcessPush(channel,event) {
    console.log("pushing:",event);
    try {
        console.log("trying to append..:",event);

           await client.appendToStream("factory",[event]);
           console.log("appending to stream");
        } catch(e) {
            console.log("game process tick failed",e);
            throw  {live: false, code: 26, event: event}
        }
        console.log("game processing...");

}

What am I doing wrong here?

rest of debug code:

processing factory failed {
  live: false,
  code: 26,
  event: { type: 'factory-creation', sub: 'sub-factory', userid: 1 }
}
result in :  undefined

Full code:

import { streamNameFilter } from "@eventstore/db-client";
import { createSecureServer } from "http2";
import { runMain } from "module";
import { eventNames, mainModule } from "process";
import { arrayBuffer } from "stream/consumers";
import { SubscriptionInfo } from "./generated/persistent_pb";
import { AllStreamPosition } from "./generated/shared_pb";

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

  
  const client = new EventStoreDBClient({
    endpoint: "localhost:2113",
}, {
    insecure: true,
});

    interface creationFactory {
        userid: string,
        location: [number,number,number],
        name: string
        type: string,
    }

class Factory {


    public async create(creation : creationFactory) {
        console.log("creating new factory.....",creation);   
        const NewFactoryEvent = CreateEvent("factory-creation",{userid: 1, sub: "sub-factory"})
        try {
            console.log("creating newfac",NewFactoryEvent);
            await gameProcessPush("factory",NewFactoryEvent)
        } catch(error) {
            console.log("processing factory failed",error);
        }
    }


}

function CreateEvent(type,data) {
    return  {type: type, sub: data.sub,
        userid: data.userid,
        
    }
}

async function   gameProcessPush(channel,event) {
    console.log("pushing:",event);
    try {
        console.log("trying to append..:",event);

           await client.appendToStream("factory",[event]);
           console.log("appending to stream");
        } catch(e) {
            console.log("game process tick failed",e);
            throw  {live: false, code: 26, event: event}
        }
        console.log("game processing...");

}


//new Factory().create({userid: "1", location: [1,1,1], name: "test bank", type: "BANK"});
async function test() {
    try {
       await  new Factory().create({
            userid: "1",
            location: [0,0,0],
            name: "test",
            type: "factory",
        });
          
    } catch(e) {
        console.log("factory creation faileD");
    }
}

test().catch((e) => {console.log("test failed",e)}).then(function (data) {
console.log("result in : ",data);
});
2 Answers

Context

EventData

An EventStore event must be a EventData object, that has, at least, two properties:

  • type, basically a string that defines the event's type.
  • data, that is the payload and must be a byte array, usually a JSON, serialized object.

appendToStream

EventStore appendToStream usage:

await client.appendToStream(STREAM_NAME, event);

Related docs

Issues

There are two potential issues in your code:

  1. You are trying to append an Array as event ([event]), instead of a EventData object.
  2. The event structure does not correspond with an EventData object, as reflected by your debug log and your CreateEvent method.

Possible solution

Process your input event to create a valid EventData object and then append it:

async function gameProcessPush(channel, event) {
  try {
    const eventData = jsonEvent({
      type: event.type,
      data: {
        sub: event.sub,
        userid: event.userid,
      },
    });
    await client.appendToStream("factory", eventData);
  } catch (e) {
    throw { live: false, code: 26, event: event };
  }
}

I am sure you've read the documentation of EventStore.

Refer if you missed something - https://developers.eventstore.com/clients/grpc/#appending-events

Try changing [event] to event

async function   gameProcessPush(channel,event) {
    console.log("pushing:",event);
    try {
        console.log("trying to append..:",event);

           await client.appendToStream("factory",event);
           console.log("appending to stream");
        } catch(e) {
            console.log("game process tick failed",e);
            throw  {live: false, code: 26, event: event}
        }
        console.log("game processing...");

}

See if it works

Related