Muhammad Irfan Muhammad Irfan - 2 months ago 14
JSON Question

parsing string from urlencode and then loop

i am sending following text in body parameter

[{"full_image":"alskdfhlkjasvbuialsdvlasljkvbaslvjhbalfdghbakjldfgjlajkshfiuqr","type":"jpg"},{"full_image": "alskdfhlkjasvbuialsdvlasljkvbaslvjhbalfdghbakjldfgjlajkshfiuqr","type":"jpg"}]


and then parsing it
JSON.pasre
but i am unable get get
lenght
and loop the json. following is the code.

var images = req.body.images;
JSON.parse(images, function(key,value){

var counter = key.length;
var seaWeedPicture = {};
var base64FullImage = value;
seaweedfs.write(new Buffer(base64FullImage, 'base64')).then(function (pic) {
var seaWeedPicture = {
picture: config.seaWeedURL + '/' + pic.fid
};

}).catch(function(err){
console.log(err);
res.format({
json: function () {
res.send({
status: 200,
data: []
});
}
});
});


the generated error is
[TypeError: must start with number, buffer, array or string]

Answer

You can get length by Object constructor

var length=Object.keys(images).length;
//Do your loop with `length`
console.log(Object.keys(images).length);//2
Comments