Closed adrian-d-hidalgo closed 4 years ago
This is the code that checks and throws that exception:
publish<T extends IEvent>(event: T): void {
const storableEvent = (event as any) as StorableEvent;
if (
storableEvent.id === undefined ||
storableEvent.eventAggregate === undefined ||
storableEvent.eventVersion === undefined
) {
throw new Error('Events must implement StorableEvent interface');
}
...
Can you log the event before publishing it? Are any of these fields undefined?
Log added:
publish(event) {
const storableEvent = event;
console.log({ storableEvent }) // <------
if (storableEvent.id === undefined ||
storableEvent.eventAggregate === undefined ||
storableEvent.eventVersion === undefined) {
throw new Error('Events must implement StorableEvent interface');
}
this.eventStore
.storeEvent(storableEvent)
.then(() => this.eventBus.publish(event))
.catch(err => {
throw err;
});
}
This is the result:
{
storableEvent: AccountCreatedEvent {
eventName: 'AccountCreatedEvent',
id: '12345',
name: 'Por Cobrar'
}
}
Isn't getting values from class attributes
export class AccountCreatedEvent extends StorableEvent {
eventAggregate: 'account';
eventVersion: 1;
constructor(public readonly id: string, public readonly name: string) {
super();
}
}
Looks like eventAggregate is undefined, thats why the Store Event Bus is throwing the exception.
eventAggregate y eventVersion are undefined, but I initialized them as attributes in the class, is there something I'm doing wrong?
export class AccountCreatedEvent extends StorableEvent {
eventAggregate: 'account'; <----
eventVersion: 1; <----
constructor(public readonly id: string, public readonly name: string) {
super();
}
}
Sorry, my bad, it was a syntax error, I used : instead of = to assign the value.
Do you have a channel where I can ask you questions regarding the implementation, I am just getting experience in Event Sourcing and CQRS, this is the first project I have to lead with these approaches.
Event is not published.
Implementation:
Event
Event Emitter
Event Handler
Expected behavior:
Display emitted event log
Current behavior:
Environment:
Node version: v13.12.0 Yarn version: v1.22.4 Typescript version: 3.7.4
package.json