While #1 returns the JSON object, #2 returns undefined.
How can I return data as JSON and access its properties (like data.username or data.email) ?
1
function username() {
user.where('id', req.id).fetch().then(function (data) {
data = data.toJSON();
console.log(data);
});
}
var adminJSON = username();
2
function username() {
user.where('id', req.id).fetch().then(function (data) {
data = data.toJSON();
return data;
});
}
var adminJSON = username();
console.log(adminJSON);