Reputation: 304
I am trying to access a json from the request like :
var files = req.files.fileData[0]; // This contains multiple file data
// initially it is like [object object]
for(var i=0; i<files.length; i++){
var fileDataArr = JSON.stringify(files[i]);
console.log("**********fileDataArr : "+fileDataArr);
var attachmentId = fileDataArr.name.attachmentId;
console.log("id : "+attachmentId);
}
The JSON structure is what i am getting while console fileDataArr is :
{"domain":null,"_events":{},"_eventsCount":0,"size":5056,"path":"C:\\Users\\k7000649\\AppData\\Local\\Temp\\8eb4f7b82b5646cef78f9989bb3353b1","name":"{\"fileName\":\"wiproNewIcon.png\",\"fileType\":\"image/png\",\"attachmentId\":\"99c148f3f5c1\",\"restrictedFileSize\":\"25690112 \"}","type":"image/png","hash":false,"lastModifiedDate":"2017-08-22T11:25:03.380Z","_writeStream":{"_writableState":{"objectMode":false,"highWaterMark":16384,"needDrain":false,"ending":true,"ended":true,"finished":true,"decodeStrings":true,"defaultEncoding":"utf8","length":0,"writing":false,"corked":0,"sync":false,"bufferProcessing":false,"writecb":null,"writelen":0,"bufferedRequest":null,"lastBufferedRequest":null,"pendingcb":0,"prefinished":true,"errorEmitted":false,"bufferedRequestCount":0,"corkedRequestsFree":{"next":null,"entry":null}},"writable":false,"domain":null,"_events":{},"_eventsCount":0,"path":"C:\\Users\\k7000649\\AppData\\Local\\Temp\\8eb4f7b82b5646cef78f9989bb3353b1","fd":null,"flags":"w","mode":438,"autoClose":true,"bytesWritten":5056,"closed":true},"length":5056,"filename":"{\"fileName\":\"wiproNewIcon.png\",\"fileType\":\"image/png\",\"attachmentId\":\"99c148f3f5c1\",\"restrictedFileSize\":\"25690112 \"}","mime":"image/png"}
The structure is valid but the backslash
I tried to parse the JSON to remove the backslash but it throws an error like SyntaxError: Unexpected token o, and when I am trying to access any data (fileDataArr.name.attachmentId) from the JSON it throws an error like :
TypeError: Cannot read property 'attachmentId' of undefined
at exports.xhr_upload_multi_attachment (D:\Harmony_Trunk\Development\Asset-Editor\build\harmony_development\routes\xhr_upload_attachment.js:122:39)
and I tried to replace the backslash using regex (/\g/,''), it's fine but I have file path in the JSON, hence file path is missing.
please advise me to resolve this issue.
Upvotes: 1
Views: 1694
Reputation: 2870
Looks like it's already parsed but it contains nested JSON data at the name
property.
Notice that the double quotes are escaped only in that property. This is because you stringified the object, which makes that property encoded a twice.
"name":"{\"fileName\":\"wiproNewIcon.png\",\"fileType\":\"image/png\",\"attachmentId\":\"99c148f3f5c1\",\"restrictedFileSize\":\"25690112 \"}"
So don't stringify at all, but parse that name
property in the loop:
var parsedName = JSON.parse(files[i].name);
console.log(parsedName.attachmentId);
If that works, then try to figure out why that one property is holding its data with an encoding different from the rest. Whatever is generating your data seems to be encoding that one property separat from the rest..
Upvotes: 3