Anand
Anand

Reputation: 195

cant access function parameters in mongoose query

I am trying to make a general function to find a model Object in mongoose Models, but its unable to access key from function parameters. Please help

    ...
    findInModel: async (Model, key, value) => {
        const modelObj = await Model.fineOne({ key: value });
    }
    ...

Upvotes: 0

Views: 43

Answers (1)

hoangdv
hoangdv

Reputation: 16127

To create a object with key is a variable, you have to use array access style.

    findInModel: async (Model, key, value) => {
        const query = {};
        query[key] = value;
        const modelObj = await Model.fineOne(query);
    }

Upvotes: 1

Related Questions