0

I have data in MongoDB structured like this

[
{
    "users": [
        "5dd13dac47b4c85e382c6e27",
        "5dce9f6d95f4ee0017be3c2c"
    ],
    "created_at": "2019-11-20T11:22:19.167Z",
    "_id": "5dd5224d76cf581424e1bb83",
    "name": "Fast Weight Loss",
    "program": [
        {
            "breakfast": [
                "3x Eggs",
                "2x Bread",
                "Cup of Milk"
            ],
            "lunch": [
                "1/4 Chicken breast"
            ],
            "dinner": [
                "1x Apple"
            ],
            "snack": [],
            "_id": "5dd5224d76cf581424e1bb84"
        }
    ],
    "__v": 0
},
{
    "users": [
        "5dd168eea514847564f04a74",
        "5dd010a1dfa846001742e913"
    ],
    "created_at": "2019-11-20T11:30:22.316Z",
    "_id": "5dd5259bcdb7af35f09e6f9e",
    "name": "30 Days Weight Loss",
    "program": [
        {
            "breakfast": [
                "3x Eggs"
            ],
            "lunch": [],
            "dinner": [],
            "snack": [],
            "_id": "5dd5259bcdb7af35f09e6f9f"
        }
    ],
    "__v": 0
}

]

I want to send a post request to my nodeJS server with a user if the userid exists in my users array this

    "users": [
    "5dd13dac47b4c85e382c6e27",
    "5dce9f6d95f4ee0017be3c2c"
],

or this

        "users": [
        "5dd168eea514847564f04a74",
        "5dd010a1dfa846001742e913"
    ],

it send the "program" array back as response here is what I have been trying:

users.post("/dietData", (req, res) => {
  var id = req.body.userID;

  DietProgram.find()
    .then(user => {
      if (user.users.contains(id)) {
        res.json(user.program);
      } else {
        res.send("User Weight Data Does not Exsist");
      }
    })
    .catch(err => {
      res.send("error: " + err);
    });
});

but I got an error that apparently I can't use contain.

1 Answer 1

1

Since users property is an Array, I think you are trying to use include()you can use it like this users.includes(id)

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.