I am working on LoopBack and Angular project. I have JSON object but I getting output null when accessing the JSON object.
I storing the object like this var file = res.result.files.file; where res has the JSON object And Accessing the Object like this var filename = file.name; which is returning Undefined,
how can I get the name form the JSON Object here is the output of the file
[{ container: 'Images',
name: '1559211341196.jpg',
type: 'multipart/form-data',
field: 'file',
size: 3577522,
providerResponse: undefined }]
form this i want to access the 'name:' Parameter, I tried this file. name, but I am getting an output as null,
how can I access the name parameter and store it in a variable?
Here is the code:
{
'use strict';
module.exports = function (Picture) {
Picture.afterRemote('upload', function (ctx, res, next) {
var http = require('request');
var file = res.result.files.file;
console.log(file);
console.log(JSON.stringify(file.container + file.name + file.size));
var i = http.get('http://192.168.1.11:3000/api/Pictures/Images/files/1559211341196.jpg', { json: true }, (body) => {
});
console.log(i.uri.href);
next();
})
};
}
Your response looks like an array of objects, so you will have to get it like this:
var file = res.result.files.file[0];
or res.result.files[0]
, it depends...
Maybe you could have several files, not just one? in that case i'd recomend to map them instead, something like...
var files = res.result.files.reduce((l, i) => ({...l, [i.name]: i}),{})
So you can have a map of files and access them with files.file1, files['187263871623.jpg']
etc
EDIT1: I'd move that require('http') out od the export, it's a module, you can share it between calls and if you have it inside the export it'll probably be called more than once.