Projection fails to process an event

Hi,

I have this user defined projection:

function linkToTenantAll(_, ev) 
{
    if (ev.eventType && !ev.eventType.startsWith("$") && ev.metadata && ev.metadata.tenant_code) {
            linkTo(ev.metadata.tenant_code + ".AllByCode", ev);
        }
        return null;
}

options({$includeLinks: false})
fromAll()
.when({$any: linkToTenantAll});

When I start this projection from the admin UI, this error shows up:

The tenantAllByCode projection failed to process an event. Handler: EventStore.Projections.Core.Services.v8.DefaultV8ProjectionStateHandler Event Position: C:24825/P:24825 Message: SyntaxError: Unexpected token ' in JSON at position 3

I only have som test events in the store. Their expected JSON shows up fine in the expected streams.

Code for adding test events:

        private class TestFixture
        {
            private struct CartItemAddedData
            {
                public Guid EventGUID { get; set; }
                public string SequenceNumber { get; set; }
                public string Description { get; set; }

                public CartItemAddedData(Guid eventGUID, string sequenceNumber, string description)
                {
                    EventGUID = eventGUID;
                    SequenceNumber = sequenceNumber;
                    Description = description;
                }
            }

            private readonly Dictionary<Guid, List<CartItemAddedData>> CartsDictionary = new Dictionary<Guid, List<CartItemAddedData>>()
            {
                {
                    Guid.Parse("d1a7c4ba-a8e4-42bb-9ec1-ef025f223aea"),
                    new List<CartItemAddedData> {
                        new CartItemAddedData(Guid.Parse("55a7f010-e41f-4cbc-9ab9-0e00fc455338"),"1","Sony Playstation 4 Pro 1TB inkl. PS Hits Naughty Dog Bundle")
                    }
                },
                {
                    Guid.Parse("b9202c4b-61ab-4c8e-8489-af72c08a1060"),
                    new List<CartItemAddedData> {
                        new CartItemAddedData(Guid.Parse("47e51c98-a811-4f24-8ec0-a9793fd9f799"), "1", "Sony PlayStation 4 slim 500 GB [incl. 2 draadloze controllers] goud"),
                        new CartItemAddedData(Guid.Parse("f979a9be-6f76-4db3-9f7d-530bd1cb7b9a"), "2", "GTA V Premium Edition"),
                        new CartItemAddedData(Guid.Parse("3090ec44-5fb8-424f-a6bc-eff898e31fa9"), "3", "The Last Of Us Part II Special Edition | PlayStation 4"),
                        new CartItemAddedData(Guid.Parse("6ca7a885-991c-44b9-b56c-add73e2d571a"), "4", "F1 2020 Seventy Edition | PlayStation 4")
                    }
                },
                {
                    Guid.Parse("0288dcce-af2d-4500-ad03-ea1c1ee4956f"),
                    new List<CartItemAddedData> {
                        new CartItemAddedData(Guid.Parse("c17888b9-05e9-4c4c-95ee-4685e85d0344"), "1", "Sony Playstation 4 1 TB [Spider-Man Limited Edition incl. draadloze controller] rood"),
                        new CartItemAddedData(Guid.Parse("a9742161-dd42-4eec-9fa7-2167dfbe78ab"), "2", "Call of Duty: Modern Warfare | PlayStation 4"),
                        new CartItemAddedData(Guid.Parse("3df75ec2-f899-4c1e-bf62-12f2b02fe685"), "3", "F1 2020 Seventy Edition | PlayStation 4")
                    }

                },
                {
                    Guid.Parse("e624f590-f17f-4106-ad47-39511e64d973"),
                    new List<CartItemAddedData>
                    {
                        new CartItemAddedData(Guid.Parse("62cd6a73-eaf7-483d-8ebd-2a26b58e4f5d"), "1", "PlayStation 4 Slim 500GB, Fortnite Edition + 2 Controller")
                    }
                }
            };

            public string TenantCode { get; }
            public IEventStoreConnection Connection { get; }

            private TestFixture()
            {
            }

            public TestFixture(string tenantCode, IEventStoreConnection connection)
            {
                TenantCode = tenantCode;
                Connection = connection;
            }

            public void AddShoppingCartsEvents()
            {
                foreach (var cartsEvents in CartsDictionary)
                {
                    var aggregateGUID = cartsEvents.Key;
                    var tenantPrefixedStreamName = $"{TenantCode}.{aggregateGUID}";

                    List<EventData> itemAddedEvents = new List<EventData>();
                    foreach (var itemAddedEvent in cartsEvents.Value)
                    {
                        var nowOffset = DateTimeOffset.UtcNow;
                        var encodedData = Encoding.UTF8.GetBytes("{'Description' : '" + itemAddedEvent.Description + "'}");
                        var encodedMetadata = Encoding.UTF8.GetBytes($@"{{
'{MetadataKeys.BOUNDED_CONTEXT_NAME}': 'ShoppingContext',
'{MetadataKeys.TENANT_CODE}': '{TenantCode}',
'{MetadataKeys.TIMESTAMP}': '{nowOffset}',
'{MetadataKeys.AGGREGATE_SEQUENCE_NUMBER}': '{itemAddedEvent.SequenceNumber}',
'{MetadataKeys.AGGREGATE_NAME}': 'ShoppingCart',
'{MetadataKeys.AGGREGATE_ID}': 'shoppingCart-{aggregateGUID}',
'{MetadataKeys.SOURCE_ID}': 'command-{aggregateGUID}',
'{MetadataKeys.EVENT_ID}': 'event-{itemAddedEvent.EventGUID}',
'{MetadataKeys.TIMESTAMP_EPOCH}': '{nowOffset.ToUnixTimeMilliseconds()}', 
'{MetadataKeys.EVENT_NAME}': 'ShoppingCartItemAdded',
'{MetadataKeys.EVENT_VERSION}': '1'
}}");

                        var eventData = new EventData(
                                itemAddedEvent.EventGUID,
                                "ShoppingCartItemAdded",
                                isJson: true,
                                data: encodedData,
                                metadata: encodedMetadata);
                        Trace.WriteLine("traced event=" + JsonConvert.SerializeObject(eventData));
                        itemAddedEvents.Add(eventData);
                    }

                    Connection.AppendToStreamAsync(
                        tenantPrefixedStreamName,
                        ExpectedVersion.Any,
                        itemAddedEvents).Wait();
                }
            }
        }

Why aren’t any of my events being handled by the projection??

I tried debugging the projection. The events are shown as:

{
    "correlationId": "ea513ba3-da22-4a05-b90a-04a8b7e51a92",
    "readerPosition": {
        "$c": 31067,
        "$p": 31067
    },
    "events": [
        {
            "eventStreamId": "1.d1a7c4ba-a8e4-42bb-9ec1-ef025f223aea",
            "eventType": "ShoppingCartItemAdded",
            "data": {
                "Description": "Sony Playstation 4 Pro 1TB inkl. PS Hits Naughty Dog Bundle"
            },
            "metadata": {
                "bounded_context_name": "ShoppingContext",
                "tenant_code": "1",
                "timestamp": "13-5-2020 11:15:57 +00:00",
                "aggregate_sequence_number": "1",
                "aggregate_name": "ShoppingCart",
                "aggregate_id": "shoppingCart-d1a7c4ba-a8e4-42bb-9ec1-ef025f223aea",
                "source_id": "command-d1a7c4ba-a8e4-42bb-9ec1-ef025f223aea",
                "event_id": "event-55a7f010-e41f-4cbc-9ab9-0e00fc455338",
                "timestamp_epoch": "1589368557858",
                "event_name": "ShoppingCartItemAdded",
                "event_version": "1"
            },
            "isJson": true,
            "readerPosition": {
                "$c": 24825,
                "$p": 24825
            }
        },
        {
            "eventStreamId": "1.b9202c4b-61ab-4c8e-8489-af72c08a1060",
            "eventType": "ShoppingCartItemAdded",
            "data": {
                "Description": "Sony PlayStation 4 slim 500 GB [incl. 2 draadloze controllers] goud"
            },
            "metadata": {
                "bounded_context_name": "ShoppingContext",
                "tenant_code": "1",
                "timestamp": "13-5-2020 11:15:58 +00:00",
                "aggregate_sequence_number": "1",
                "aggregate_name": "ShoppingCart",
                "aggregate_id": "shoppingCart-b9202c4b-61ab-4c8e-8489-af72c08a1060",
                "source_id": "command-b9202c4b-61ab-4c8e-8489-af72c08a1060",
                "event_id": "event-47e51c98-a811-4f24-8ec0-a9793fd9f799",
                "timestamp_epoch": "1589368558893",
                "event_name": "ShoppingCartItemAdded",
                "event_version": "1"
            },
            "isJson": true,
            "readerPosition": {
                "$c": 25526,
                "$p": 25526
            }
        },
        {
            "eventStreamId": "1.b9202c4b-61ab-4c8e-8489-af72c08a1060",
            "eventNumber": 1,
            "eventType": "ShoppingCartItemAdded",
            "data": {
                "Description": "GTA V Premium Edition"
            },
            "metadata": {
                "bounded_context_name": "ShoppingContext",
                "tenant_code": "1",
                "timestamp": "13-5-2020 11:15:58 +00:00",
                "aggregate_sequence_number": "2",
                "aggregate_name": "ShoppingCart",
                "aggregate_id": "shoppingCart-b9202c4b-61ab-4c8e-8489-af72c08a1060",
                "source_id": "command-b9202c4b-61ab-4c8e-8489-af72c08a1060",
                "event_id": "event-f979a9be-6f76-4db3-9f7d-530bd1cb7b9a",
                "timestamp_epoch": "1589368558894",
                "event_name": "ShoppingCartItemAdded",
                "event_version": "1"
            },
            "isJson": true,
            "readerPosition": {
                "$c": 26235,
                "$p": 26235
            }
        },
        {
            "eventStreamId": "1.b9202c4b-61ab-4c8e-8489-af72c08a1060",
            "eventNumber": 2,
            "eventType": "ShoppingCartItemAdded",
            "data": {
                "Description": "The Last Of Us Part II Special Edition | PlayStation 4"
            },
            "metadata": {
                "bounded_context_name": "ShoppingContext",
                "tenant_code": "1",
                "timestamp": "13-5-2020 11:15:58 +00:00",
                "aggregate_sequence_number": "3",
                "aggregate_name": "ShoppingCart",
                "aggregate_id": "shoppingCart-b9202c4b-61ab-4c8e-8489-af72c08a1060",
                "source_id": "command-b9202c4b-61ab-4c8e-8489-af72c08a1060",
                "event_id": "event-3090ec44-5fb8-424f-a6bc-eff898e31fa9",
                "timestamp_epoch": "1589368558894",
                "event_name": "ShoppingCartItemAdded",
                "event_version": "1"
            },
            "isJson": true,
            "readerPosition": {
                "$c": 26898,
                "$p": 26898
            }
        },
        {
            "eventStreamId": "1.b9202c4b-61ab-4c8e-8489-af72c08a1060",
            "eventNumber": 3,
            "eventType": "ShoppingCartItemAdded",
            "data": {
                "Description": "F1 2020 Seventy Edition | PlayStation 4"
            },
            "metadata": {
                "bounded_context_name": "ShoppingContext",
                "tenant_code": "1",
                "timestamp": "13-5-2020 11:15:58 +00:00",
                "aggregate_sequence_number": "4",
                "aggregate_name": "ShoppingCart",
                "aggregate_id": "shoppingCart-b9202c4b-61ab-4c8e-8489-af72c08a1060",
                "source_id": "command-b9202c4b-61ab-4c8e-8489-af72c08a1060",
                "event_id": "event-6ca7a885-991c-44b9-b56c-add73e2d571a",
                "timestamp_epoch": "1589368558894",
                "event_name": "ShoppingCartItemAdded",
                "event_version": "1"
            },
            "isJson": true,
            "readerPosition": {
                "$c": 27594,
                "$p": 27594
            }
        },
        {
            "eventStreamId": "1.0288dcce-af2d-4500-ad03-ea1c1ee4956f",
            "eventType": "ShoppingCartItemAdded",
            "data": {
                "Description": "Sony Playstation 4 1 TB [Spider-Man Limited Edition incl. draadloze controller] rood"
            },
            "metadata": {
                "bounded_context_name": "ShoppingContext",
                "tenant_code": "1",
                "timestamp": "13-5-2020 11:15:58 +00:00",
                "aggregate_sequence_number": "1",
                "aggregate_name": "ShoppingCart",
                "aggregate_id": "shoppingCart-0288dcce-af2d-4500-ad03-ea1c1ee4956f",
                "source_id": "command-0288dcce-af2d-4500-ad03-ea1c1ee4956f",
                "event_id": "event-c17888b9-05e9-4c4c-95ee-4685e85d0344",
                "timestamp_epoch": "1589368558897",
                "event_name": "ShoppingCartItemAdded",
                "event_version": "1"
            },
            "isJson": true,
            "readerPosition": {
                "$c": 28275,
                "$p": 28275
            }
        },
        {
            "eventStreamId": "1.0288dcce-af2d-4500-ad03-ea1c1ee4956f",
            "eventNumber": 1,
            "eventType": "ShoppingCartItemAdded",
            "data": {
                "Description": "Call of Duty: Modern Warfare | PlayStation 4"
            },
            "metadata": {
                "bounded_context_name": "ShoppingContext",
                "tenant_code": "1",
                "timestamp": "13-5-2020 11:15:58 +00:00",
                "aggregate_sequence_number": "2",
                "aggregate_name": "ShoppingCart",
                "aggregate_id": "shoppingCart-0288dcce-af2d-4500-ad03-ea1c1ee4956f",
                "source_id": "command-0288dcce-af2d-4500-ad03-ea1c1ee4956f",
                "event_id": "event-a9742161-dd42-4eec-9fa7-2167dfbe78ab",
                "timestamp_epoch": "1589368558897",
                "event_name": "ShoppingCartItemAdded",
                "event_version": "1"
            },
            "isJson": true,
            "readerPosition": {
                "$c": 29001,
                "$p": 29001
            }
        },
        {
            "eventStreamId": "1.0288dcce-af2d-4500-ad03-ea1c1ee4956f",
            "eventNumber": 2,
            "eventType": "ShoppingCartItemAdded",
            "data": {
                "Description": "F1 2020 Seventy Edition | PlayStation 4"
            },
            "metadata": {
                "bounded_context_name": "ShoppingContext",
                "tenant_code": "1",
                "timestamp": "13-5-2020 11:15:58 +00:00",
                "aggregate_sequence_number": "3",
                "aggregate_name": "ShoppingCart",
                "aggregate_id": "shoppingCart-0288dcce-af2d-4500-ad03-ea1c1ee4956f",
                "source_id": "command-0288dcce-af2d-4500-ad03-ea1c1ee4956f",
                "event_id": "event-3df75ec2-f899-4c1e-bf62-12f2b02fe685",
                "timestamp_epoch": "1589368558897",
                "event_name": "ShoppingCartItemAdded",
                "event_version": "1"
            },
            "isJson": true,
            "readerPosition": {
                "$c": 29687,
                "$p": 29687
            }
        },
        {
            "eventStreamId": "1.e624f590-f17f-4106-ad47-39511e64d973",
            "eventType": "ShoppingCartItemAdded",
            "data": {
                "Description": "PlayStation 4 Slim 500GB, Fortnite Edition + 2 Controller"
            },
            "metadata": {
                "bounded_context_name": "ShoppingContext",
                "tenant_code": "1",
                "timestamp": "13-5-2020 11:15:58 +00:00",
                "aggregate_sequence_number": "1",
                "aggregate_name": "ShoppingCart",
                "aggregate_id": "shoppingCart-e624f590-f17f-4106-ad47-39511e64d973",
                "source_id": "command-e624f590-f17f-4106-ad47-39511e64d973",
                "event_id": "event-62cd6a73-eaf7-483d-8ebd-2a26b58e4f5d",
                "timestamp_epoch": "1589368558899",
                "event_name": "ShoppingCartItemAdded",
                "event_version": "1"
            },
            "isJson": true,
            "readerPosition": {
                "$c": 30368,
                "$p": 30368
            }
        },
        {
            "eventStreamId": "2.d1a7c4ba-a8e4-42bb-9ec1-ef025f223aea",
            "eventType": "ShoppingCartItemAdded",
            "data": {
                "Description": "Sony Playstation 4 Pro 1TB inkl. PS Hits Naughty Dog Bundle"
            },
            "metadata": {
                "bounded_context_name": "ShoppingContext",
                "tenant_code": "2",
                "timestamp": "13-5-2020 11:15:58 +00:00",
                "aggregate_sequence_number": "1",
                "aggregate_name": "ShoppingCart",
                "aggregate_id": "shoppingCart-d1a7c4ba-a8e4-42bb-9ec1-ef025f223aea",
                "source_id": "command-d1a7c4ba-a8e4-42bb-9ec1-ef025f223aea",
                "event_id": "event-55a7f010-e41f-4cbc-9ab9-0e00fc455338",
                "timestamp_epoch": "1589368558901",
                "event_name": "ShoppingCartItemAdded",
                "event_version": "1"
            },
            "isJson": true,
            "readerPosition": {
                "$c": 31067,
                "$p": 31067
            }
        }
    ]
}

I can than ‘Run step’ through all the events and each time it shows the expected state, untill the button get’s disabled. Than I get this:

These events keep showing up with a different correlationId. What does that mean?

My 5.0.8.0 eventstore runs in a linux based container on latest docker desktop for windows. I am using the 5.0.8.0 .Net Client API.

Kind regards

Snap, got it!
Only double quotes are considered valid JSON :crazy_face:.

These two lines fixed my projection not handling any events…

var eventData = Encoding.UTF8.GetBytes("{\"Description\":\"" + itemAddedEventData.Description + "\"}");
var eventMetadata = Encoding.UTF8.GetBytes("{\"" + MetadataKeys.TENANT_CODE + "\":\"" + TenantCode + "\"}");

Is it appropriate to persist an event using AppendToStreamAsync with isJson: true, that really isn’t valid JSON during parsing in the projection?

Cheers

1 Like