I am trying to accept the serialized value as the user input and de-serialize the same on the server.
Code
var express = require('express');
var app = express();
var cookieParser = require('cookie-parser');
app.use(cookieParser())
var JSONAPIDeserializer = require('jsonapi-serializer').Deserializer;
var port = process.env.PORT || 1337;
var bodyParser = require('body-parser');
app.get('/', function(req, res) {
var data = new Buffer(req.cookies.profile, 'base64').toString();
var UserDeserialize = new JSONAPIDeserializer();
UserDeserialize.deserialize(data).then(result => {
console.log(result);
res.json(result);
});
});
app.listen(port);
console.log('Server started! At http://localhost:' + port);
Request
GET / HTTP/1.1
Host: localhost:1337
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:52.0) Gecko/20100101 Firefox/52.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: close
Cookie: profile=eyBkYXRhOiBbIHsgdHlwZTogJ2lkcycsIGlkOiAnMScsIGF0dHJpYnV0ZXM6IFtPYmplY3RdIH0gXSB9
Upgrade-Insecure-Requests: 1
X-Forwarded-For: 127.0.0.1
Here
profile=eyBkYXRhOiBbIHsgdHlwZTogJ2lkcycsIGlkOiAnMScsIGF0dHJpYnV0ZXM6IFtPYmplY3RdIH0gXSB9
is serialized value base64 encoded of
{ data: [ { type: 'ids', id: '1', attributes: [Object] } ] }
Response is
HTTP/1.1 500 Internal Server Error
X-Powered-By: Express
Content-Security-Policy: default-src 'self'
X-Content-Type-Options: nosniff
Content-Type: text/html; charset=utf-8
Content-Length: 1882
Date: Sun, 12 Mar 2017 09:25:33 GMT
Connection: close
<pre>TypeError: Cannot read property 'attributes' of undefined<br> at extractAttributes (/Users/acid/node_js/node_modules/jsonapi-serializer/lib/deserializer-utils.js:70:36)<br> at module.exports.perform (/Users/acid/node_js/node_modules/jsonapi-serializer/lib/deserializer-utils.js:123:13)<br> at resource
I am expecting the result would be like this:-
[ { id: '1' } ]
What I am doing incorrect here ?
via Ashu
No comments:
Post a Comment