vadimdemedes / mongorito

🍹 MongoDB ODM for Node.js apps based on Redux
1.38k stars 90 forks source link

Concatenated Array fields on set #179

Open EdenCoder opened 7 years ago

EdenCoder commented 7 years ago
        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?

vadimdemedes commented 7 years ago

Oh yes, you are right!

Mywifi commented 6 years ago

i meet the same problem, can you fix this?

vadimdemedes commented 6 years ago

@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?

LennyPenny commented 6 years ago

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

LennyPenny commented 6 years ago

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);
    };
};