BUG? State not being modified in projection

Hello,

I’m new to EventStore. I’ve been trying to use the projections example, from multiple streams. I named the streams user-(uuid4) and each of them has one UserCreated event.

The problem is that userCount does not increase. On the projection page both state and results are empty, but when I go to debug and do step-by-step, I see the initial state as 0, but no matter how many events I play, it doesn’t change.

On debug mode, I can see the event list in the category properly and every time I run a step it gets reduced. I tried to console.log before and after the increment and the variable is being properly changed, but it seems that the scope is local.

I’m following this approach (http://docs.geteventstore.com/introduction/3.8.1/projections/):

fromCategory('user')
  .foreachStream()
  .when({
    '$init': function(state, ev) {
      return { userCount: 0 }
    },
    'UserCreated': function(state, ev) {
      state.userCount++
    }
  })

There is an explanation about what you are seeing in this issue https://github.com/EventStore/EventStore/issues/699
If you have any further issues, please don’t hesitate to shout.

Thanks for super fast reply.

I read what you mentioned. What makes it very weird is that I’m using the ES UI, and there is a state and results fields and they’re both empty. And I can’t select the partition from there. I dont want to partition BTW, I want to count all events from all streams from this category

It works with fromAll, but it doesn’t specify at all that this is from my user category. What if in another context I have event with the same name?

If you don’t want to create a partition, you can just change your projection to not use foreachStream.
e.g.

fromCategory('user')
  .when({
    '$init': function(state, ev) {
      return { userCount: 0 }
    },
    'UserCreated': function(state, ev) {
      state.userCount++

    }
  })

``