0

I want to create a json object according to function parameters but it takes the parameters as a string.

userSchema.statics.updated = async function (_id, field, value) { // it does not take that params
  const user = await this.updateOne({ _id }, { $set: { field: value } }); //!!!!
};

The field parameters is a json field that i create like name,age... But it always create and object like

{"field":value}

how can i fix it?

Json structure example:

{
        "email": "",
        "password": "",
        "surname": "",
        "name": "",
        "nickname": "",
        "birthdate": ""
    }
1
  • 2
    What you're looking for is { [field]: value }if I understand correctly. The key must be dynamic, but right now you're marking it explicitly as field. Commented Jan 9, 2021 at 17:42

1 Answer 1

1

You are passing the string "field" as the value field, Try pass a real field name, for example:

const jsonFile = require('jsonFile');
const object = JSON.parse(jsonFile);
fieldName = 'email';
userSchema.statics.updated(_id, object[fieldName], false));

// You can also:

const keys = Object.keys(object);
userSchema.statics.updated(_id, object[keys[0]], false));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.