I apologize if answers exist elsewhere, please redirect me. I'm using node.js, express, & ejs.
server.js
//API v4//media
const mediaAPI = { method: 'GET',
url: 'https://api.media.com/v4/media',
qs:
{ access_token: 'abc123'
}
};
//media data
app.get('/media', function (req, res) {
try {
request(mediaAPI, function (error, response, body) {
if (error) throw new Error(error);
const mediaphotos = JSON.parse(body);
const mediaphotolist = mediaphotos.response.checkins.items.map(item => item );
res.render('media.ejs', {mediaphotolist});
console.log(mediaphotolist);
});
} catch(e) {
console.log("Something went wrong", e)
}
});
Based on this code, I can see the following array in my console.log(mediaphotolist). Please note, there are multiple items returned, I'm only showing one to save space.
[ { uid: 123456789,
person:
{ uid: 1234,
first_name: 'frank',
last_name: 'doe',
contact: [Object] },
house:
{ hid: 5724,
house_name: 'Peterson House',
house_style: 'Modern' },
neighborhood:
{ nid: 379,
neighborhood_name: 'Dexter',
neighborhood_type: 'Old' },
media: { count: 1, items: [Array] }
} ]
There is an image URL inside of the media: [Array] that I want to get. As seen inside postman, it looks like this...
media: {
count: 1,
items: [
{
photo_id: 1234,
photo: {
photo_img_sm: https://media.net/photos/123_200x200.jpg,
photo_img_md: https://media.net/photos/456_640x640.jpg,
photo_img_lg: https://media.net/photos/789_1280x1280.jpg
}
}
]
}
In my media.ejs file I have the following ejs code to display first_name and photo_img_sm...
media.ejs
<ul>
<% mediaphotolist.forEach(function(photoofhouse){ %>
<div>
<li>
<p>First Name: <%= photoofhouse.person.first_name %></p>
<img src= "<%= photoofhouse.media.items.photo.photo_img_sm %>" alt="">
</li>
</div>
<% }) %>
</ul>
I can make the first_name show using the ejs script seen above, but, I don't know know to dig deeper to get the photo_img_sm object that lives within these nested arrays and objects. I'm hoping I don't have to change my server side code and simply learn the proper syntax for accessing this photo_img_sm object.