0

I am trying to achieve to make a object from a string.

location=new%20york&start_date=2020-06-01T00%3A00&end_date=2020-06-24T23%3A59&type=ADD&actor_uuid=f4927094-a682-4817-a6e4-03f4f6cb5h62&group_uuid=a8fc84a5-ab4d-7455-ydu5-ccbbcd2ddc79&group_uuid=af372af8-6e38-40fb-9e2e-adc5425ec50c&access_name=admin

So the above is the string I am having it from front and processing it in backend want to store it in mongodb using mongoose

My request body:

{
    "City": "New York",   
    "type": "BILLED",
    "details":"location=new%20york&start_date=2020-06-01T00%3A00&end_date=2020-06-24T23%3A59&type=ADD&actor_uuid=f4927094-a682-4817-a6e4-03f4f6cb5h62&group_uuid=a8fc84a5-ab4d-7455-ydu5-ccbbcd2ddc79&group_uuid=af372af8-6e38-40fb-9e2e-adc5425ec50c&access_name=admin"
}

Expected output respose -

 {
  City: "New York",
  type: "BILLED",
  details: {
    And: [
      {
        _id: "objectid",
        field: "location",
        value: "new york",
        operator: "equal"
      },
      {
        _id: "objectid",
        field: "start_date",
        value: "2020-06-01T00 3A00",
        operator: "equal"
      },
      {
        _id: "objectid",
        field: "end_date",
        value: "2020-06-24T23 3A59",
        operator: "equal"
      },
      {
        _id: "objectid",
        field: "type",
        value: "ADD",
        operator: "equal"
      },
      {
        _id: "objectid",
        field: "actor_uuid",
        value: "f4927094-a682-4817-a6e4-03f4f6cb5h62",
        operator: "equal"
      },
      {
        _id: "objectid",
        field: "group_uuid",
        value: "a8fc84a5-ab4d-7455-ydu5-ccbbcd2ddc79",
        operator: "equal"
      },
      {
        _id: "objectid",
        field: "group_uuid",
        value: "af372af8-6e38-40fb-9e2e-adc5425ec50c",
        operator: "equal"
      },
      {
        _id: "objectid",
        field: "access_name",
        value: "admin",
        operator: "equal"
      }
    ]
  }

Where I want to store it under conditions something like above, Now How can I achieve this? Also how do I make sure and prevent sql injections?

Tech Nerd
  • 93
  • 3
  • 11
  • 1
    With `URLSearchParams`, you have the right primitive to transform data as you like. Example: `const params = new URLSearchParams(object.details); [...params].forEach(([key, value]) => {console.log(key, '=', value)})`. – Andrea Giammarchi Jul 07 '20 at 09:51
  • Why dont you use `querystring` module ? `const qs = require('querystring'); let object = qs.parse(details)` – MahanTp Jul 07 '20 at 09:55
  • @Mahan `Why dont you use querystring module ?`: Because there is no need for that since URLSearchParams is a native JavaScript object. – HMR Jul 07 '20 at 09:59
  • Not getting the expected output, Can you help – Tech Nerd Jul 07 '20 at 10:10
  • Does this answer your question? [How to convert URL parameters to a JavaScript object?](https://stackoverflow.com/questions/8648892/how-to-convert-url-parameters-to-a-javascript-object) – ShinaBR2 Jul 07 '20 at 10:32
  • How can I achieve the above scenario? – Tech Nerd Jul 07 '20 at 10:48

0 Answers0