Sahan
Sahan

Reputation: 1467

toJSON() does not work on a array full of json objects

UPDATED QUESTION

Im using sails.js v0.10.5

And I'm trying to use sails toJSON() and im trying to delete some stuff from the server before it is sent to the client.

So I have a user look up system where someone can write a name and then Typeaead will query my database using Sails as the backend. Now when the suggestions are sent as an array which includes multiple users as objects.

the returned object is as follows

[
  {
    "_id": "55394747d01d6777e09f4919",
    "index": 0,
    "guid": "1e08f17d-0163-40f6-b673-c9b1823b3e9b",
    "isActive": true,
    "balance": "$2,994.39",
    "picture": "http://placehold.it/32x32",
    "age": 28,
    "eyeColor": "brown",
    "name": "Grant Hughes",
    "gender": "male",
    "company": "CUBIX",
    "email": "[email protected]",
    "phone": "+1 (972) 415-2367",
    "address": "656 Hornell Loop, Farmers, New York, 7735",
    "about": "Elit aute veniam commodo do dolore labore ullamco reprehenderit ut exercitation magna laboris nulla. Proident qui proident labore proident anim in anim reprehenderit non officia sint amet. Voluptate ea amet nostrud mollit deserunt magna laboris aute laboris deserunt ea quis Lorem nulla. Ullamco ea aliquip laborum qui ut dolor nisi ad id irure in occaecat. Dolore est amet adipisicing sit.\r\n",
    "registered": "2014-04-07T02:21:28 -12:00",
    "latitude": 52.890391,
    "longitude": 67.80552
  },
  {
    "_id": "55394747f2a80d1ba9b01b5e",
    "index": 1,
    "guid": "6030d442-b540-4879-9a2d-5faba79d67a7",
    "isActive": false,
    "balance": "$3,230.72",
    "picture": "http://placehold.it/32x32",
    "age": 28,
    "eyeColor": "brown",
    "name": "Nolan Carr",
    "gender": "male",
    "company": "PEARLESSA",
    "email": "[email protected]",
    "phone": "+1 (957) 464-3471",
    "address": "529 Laurel Avenue, Springville, Colorado, 8055",
    "about": "Labore nostrud culpa ad anim ea amet. Exercitation nostrud exercitation et ad. Ad laboris dolore cupidatat aute Lorem cupidatat consequat nulla. Eiusmod ad et in laborum irure. Ad ullamco sit voluptate officia officia sint magna mollit esse duis minim cillum. Ex incididunt sint labore consequat id sint mollit ex incididunt pariatur laborum dolor reprehenderit.\r\n",
    "registered": "2015-02-02T16:50:50 -13:00",
    "latitude": -53.719292,
    "longitude": -95.203951
  },
  {
    "_id": "55394747ba2b581b96837ffd",
    "index": 2,
    "guid": "69edd6e6-b100-4139-bde5-afb6e4843809",
    "isActive": false,
    "balance": "$3,596.99",
    "picture": "http://placehold.it/32x32",
    "age": 33,
    "eyeColor": "green",
    "name": "Phoebe Case",
    "gender": "female",
    "company": "POLARIUM",
    "email": "[email protected]",
    "phone": "+1 (850) 498-2504",
    "address": "954 Roder Avenue, Day, North Carolina, 7135",
    "about": "Aute occaecat aute dolore proident ipsum et adipisicing adipisicing. Nostrud voluptate irure in proident magna. Enim anim ut do esse nostrud culpa enim deserunt amet laborum cupidatat.\r\n",
    "registered": "2014-08-30T23:07:38 -12:00",
    "latitude": 4.607936,
    "longitude": 3.118566
  },
  {
    "_id": "5539474726b1eb741927212d",
    "index": 3,
    "guid": "9ac1c59b-9579-4d5a-916d-76b4e1f77ec6",
    "isActive": true,
    "balance": "$1,735.99",
    "picture": "http://placehold.it/32x32",
    "age": 31,
    "eyeColor": "brown",
    "name": "Zimmerman Holder",
    "gender": "male",
    "company": "SNOWPOKE",
    "email": "[email protected]",
    "phone": "+1 (804) 472-3444",
    "address": "290 Rock Street, Eureka, Wyoming, 2608",
    "about": "Laborum occaecat est elit irure Lorem sunt eu velit proident occaecat commodo duis. Dolor amet sunt labore culpa consequat consectetur officia sit veniam. Sunt irure et ex sunt reprehenderit excepteur consectetur ea. Nostrud id excepteur irure officia commodo.\r\n",
    "registered": "2014-10-07T22:49:40 -13:00",
    "latitude": 60.592749,
    "longitude": -2.034919
  },
  {
    "_id": "553947472855b7e80715fdf0",
    "index": 4,
    "guid": "8e67b7d2-a88b-4472-bf6b-17e893e4bb19",
    "isActive": true,
    "balance": "$1,264.71",
    "picture": "http://placehold.it/32x32",
    "age": 38,
    "eyeColor": "green",
    "name": "Cooley Mcdonald",
    "gender": "male",
    "company": "PLAYCE",
    "email": "[email protected]",
    "phone": "+1 (863) 527-3596",
    "address": "741 Virginia Place, Watrous, Kentucky, 6411",
    "about": "Irure reprehenderit do quis id cillum fugiat. Qui sunt voluptate culpa anim officia occaecat consequat aute et. Amet consectetur minim deserunt do ea consequat nulla dolore. Esse nisi commodo cillum exercitation. Culpa est minim esse ad commodo ipsum sit. Nostrud deserunt pariatur commodo laboris esse id officia enim mollit aliqua.\r\n",
    "registered": "2014-12-15T17:15:37 -13:00",
    "latitude": 31.826787,
    "longitude": 168.934892
  },
  {
    "_id": "55394747d5875e0e9847aa1f",
    "index": 5,
    "guid": "633d45bd-a417-44eb-b16e-4dd40b08c2fd",
    "isActive": false,
    "balance": "$3,369.67",
    "picture": "http://placehold.it/32x32",
    "age": 36,
    "eyeColor": "blue",
    "name": "Conner Foley",
    "gender": "male",
    "company": "ESSENSIA",
    "email": "[email protected]",
    "phone": "+1 (889) 402-3506",
    "address": "864 Stryker Street, Denio, Florida, 447",
    "about": "Veniam esse nisi eiusmod ex Lorem. Duis do deserunt sunt aliquip officia. Consequat proident voluptate irure Lorem fugiat labore. Voluptate tempor officia aute dolor anim velit dolor officia mollit consequat. Qui officia exercitation ut et veniam deserunt ea proident aliquip.\r\n",
    "registered": "2014-08-02T02:33:46 -12:00",
    "latitude": 81.51229,
    "longitude": 86.08351
  },
  {
    "_id": "5539474795bbdfa4c3f2fc80",
    "index": 6,
    "guid": "928c237f-6d34-4a29-ae10-121113920041",
    "isActive": true,
    "balance": "$1,066.25",
    "picture": "http://placehold.it/32x32",
    "age": 25,
    "eyeColor": "blue",
    "name": "Imogene Riggs",
    "gender": "female",
    "company": "ZENTIME",
    "email": "[email protected]",
    "phone": "+1 (901) 508-3706",
    "address": "261 Vanderbilt Street, Chamberino, Washington, 4035",
    "about": "Proident ex enim anim adipisicing culpa. Eu qui ad mollit duis dolor do. Elit cillum officia amet amet id aliqua Lorem amet labore adipisicing voluptate magna. Id elit amet duis consequat. Voluptate excepteur dolore exercitation tempor quis id dolor ea aute. Voluptate velit amet officia adipisicing cillum in aliquip ut irure ad velit Lorem quis.\r\n",
    "registered": "2014-06-24T09:34:41 -12:00",
    "latitude": -49.444024,
    "longitude": -112.470791
  }
]

I need the toJSON to work and remove some sensitive information from these each user found, So I only want the sails backend to send me the age, name, _id to the frontend,client.

Please help

Upvotes: 1

Views: 71

Answers (1)

Ryan W
Ryan W

Reputation: 6173

Sounds like you want to parse your database query results into JSON then you can modified it.

If thats correct you can use JSON.parse()

example:

   var usersJSON = JSON.parse(userModels)

Upvotes: 1

Related Questions