Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kylebradshaw/95fc6ec323635988f039 to your computer and use it in GitHub Desktop.
Save kylebradshaw/95fc6ec323635988f039 to your computer and use it in GitHub Desktop.
ROBOHYDRA PLUGIN CODE:
...
new RoboHydraHead({
path: '/services/users',
handler: function(req, res) {
if (req.method === 'POST') {
var newUser;
var rand = Math.floor(Math.random() * (1000 - 100)) + 100;
for (var i in req.bodyParams) {
newUser = JSON.parse(i); //error occurs on this line when I try to do this and there is a . in an email address
newUser.id = rand;
newUser.uid = rand;
newUser.name = newUser.firstName + " " + newUser.lastName;
newUser.username = newUser.emailAddress;
newUser.created = (new Date()).getTime();
newUser.modified = (new Date()).getTime();
newUser.status = "ACTIVE";
newUser.enabled = true;
}
//add new user to Users collection
console.log(newUser);
// Users.push(newUser);
res.statusCode = 200;
res.send(JSON.stringify(newUser));
res.end();
}
}
}),
...
POST DATA (via postman to endpoint)
{
"emailAddress": "test@test\U002Ecom",
"firstName": "Test",
"lastName": "Testing"
}
SUCCESSFUL OUTPUT (from console.log)
{
"emailAddress": "test@test.com",
"firstName": "Test",
"lastName": "Testing",
"id": 373,
"uid": 373,
"name": "Test Testing",
"username": "test@test.com",
"created": 1441983431588,
"modified": 1441983431588,
"status": "ACTIVE",
"enabled": true
}
If I change the email address to test@test.com and POST
{
"emailAddress": "test@test.com",
"firstName": "Test",
"lastName": "Testing"
}
SyntaxError: Unexpected end of input
at Object.parse (native)
at AbstractRoboHydraHead.heads.RoboHydraHead.handler (/Users/kbradsha/Development/idiom-ui/robohydra/plugins/idiom/index.js:160:38)
at AbstractRoboHydraHead.RoboHydraHead.handle (/Users/kbradsha/Development/idiom-ui/node_modules/robohydra/lib/heads.js:205:18)
at RoboHydra.handle (/usr/local/lib/node_modules/robohydra/lib/RoboHydra.js:289:20)
at /usr/local/lib/node_modules/robohydra/lib/RoboHydra.js:297:22
at AbstractRoboHydraHead.conf.robohydra.registerDynamicHead.RoboHydraHead.handler (/usr/local/lib/node_modules/robohydra/plugins/cors/index.js:42:17)
at AbstractRoboHydraHead.RoboHydraHead.handle (/usr/local/lib/node_modules/robohydra/lib/heads.js:205:18)
at RoboHydra.handle (/usr/local/lib/node_modules/robohydra/lib/RoboHydra.js:289:20)
at IncomingMessage.
<anonymous> (/usr/local/lib/node_modules/robohydra/lib/robohydraserver.js:37:53)
at IncomingMessage.emit (events.js:104:17)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment