Hi,
Today im trying to get some data from API.
But i don’'t know how to transform result of http request as json. I’m getting this instead: [object Promise]
My code is as follows, and my issue is below // This console will return [object Promise] comment.
Thanks for advice.
var Observable = require(“data/observable”).Observable;
var pageData = new Observable();
function loaded(args) {
var page = args.object;
// Get navigation context
var gotData=page.navigationContext;
var http = require("http");
var x;
var a = http.getJSON("https://jsonplaceholder.typicode.com/posts/1").then(function (r) {
//console.log(a);
}, function (e) {
//console.log(e)
});
// This console will return [object Promise]
console.log(a);
// Create new observable object model
var model = new Observable();
// get id from prevoius frame context, convert to number first
var selectedId = Number.parseInt(gotData.id);
// Just assign id to observable
model.id = selectedId;
// Dummy array of projects
var project = [
{ id:11, name:'Arcade Filter', desc: "Build a Rocket Boys!", image:'~/images/show/1.jpg'},
{ id:12, name:'Arcade Filter2', desc: "Build a Rocket Boys!", image:'~/images/show/1.jpg'},
{ id:13, name:'Arcade Filter3', desc: "Build a Rocket Boys!", image:'~/images/show/1.jpg'},
];
// Project with found id, now search for this id and get all infos
var item = project.find(item => item.id === selectedId);
// Bind rest of details.
model.name = item.name;
model.desc = x;
model.image = item.image;
// Bind model object
page.bindingContext = model;
};
// export data
exports.loaded = loaded;