vadimdemedes/mongorito

Concatenated Array fields on set

Opened this issue · 5 comments

		case ActionTypes.SET:
			return merge(state, action.fields);

When using set on a field of type Array, the field is concatenated. It should be overwritten shouldn't it?

Oh yes, you are right!

i meet the same problem, can you fix this?

@Mywifi I don't have enough time for this, but I'd happily help you resolve this. Would you be interested in making a PR for it?

How trivial would this be? is a lodash.set enough?

I fixed this temporarily with a middleware as I'm not knowledgable enough with lodash and reactive stuff to fix the underlying issue
Here it is if you need it @EdenCoder

import { ActionTypes } from "mongorito";

export default () => {
    return ({ model }) => next => action => {
        if (action.type === ActionTypes.SET) {
            for (const key in action.fields) {
                if (Array.isArray(action.fields[key])) {
                    model.unset(key);
                }
            }
        }

        return next(action);
    };
};