I've never worked on this sort of thing before and I kind of jumped in the deep end, but I feel like I'm really close. I'm trying to add a dynamically populated select list to the edit dialog of a node red node. I wrote a method in the client that calls a method I wrote in the server. The call executes. The server retrieves the data (confirmed via debug print to the console), but the client receives something other than the data. I've not dont much javascript before, but according to my understanding of promises, it seems like the data should be sent to the client, so I don't understand what I'm missing.
The server method in server.js:
getCircles() {
var node = this;
return node.updateSession(function (session) {
return life360.circles(session)
.then(circles => {
console.log("this gets called after the end of the main stack. the value received and returned is: " + JSON.stringify(circles));
return circles;
})
});
}
The server "requires" ../index.js, which has:
/**
* Fetch the user's circles
*/
module.exports.circles = function (session) {
if (!session) throw new Error("session not specified");
return new Promise((resolve, reject) => {
const LIFE360_CIRCLES_URL = "https://api.life360.com/v3/circles"
const options = {
url: LIFE360_CIRCLES_URL,
headers: {
'Authorization': `${session.token_type} ${session.access_token}`
},
json: true
};
// console.log(`Fetching circles at ${LIFE360_CIRCLES_URL}`);
request(options)
.then(response => {
let circles = response.body.circles;
console.log("Returning circles: " + JSON.stringify(circles));
resolve(circles);
});
});
}
The client method that calls the server method (getCircles
) in location.js:
let circles = node.server.getCircles();
console.log("circles = " + JSON.stringify(circles));
Here's what I get in the log (I redacted the ids, though I don't know if I needed to):
circles = {"isFulfilled":false,"isRejected":false}
Returning circles: [{"id":"xxxxxxxx","name":"Rob's Homes","color":"da22d5","type":"basic","createdAt":"1379025809","memberCount":"4","unreadMessages":"0","unreadNotifications":"0","features":{"ownerId":null,"skuId":null,"premium":"0","locationUpdatesLeft":0,"priceMonth":"0","priceYear":"0","skuTier":null}},{"id":"xxxxxxxx","name":"Rob's Family","color":"f05929","type":"basic","createdAt":"1393782349","memberCount":"4","unreadMessages":"0","unreadNotifications":"0","features":{"ownerId":null,"skuId":null,"premium":"0","locationUpdatesLeft":0,"priceMonth":"0","priceYear":"0","skuTier":null}},{"id":"xxxxxxxx","name":"Leach Family","color":"7f26c2","type":"basic","createdAt":"1393785316","memberCount":"6","unreadMessages":"0","unreadNotifications":"0","features":{"ownerId":null,"skuId":null,"premium":"0","locationUpdatesLeft":0,"priceMonth":"0","priceYear":"0","skuTier":null}}]
this gets called after the end of the main stack. the value received and returned is: [{"id":"xxxxxxxx","name":"Rob's Homes","color":"da22d5","type":"basic","createdAt":"1379025809","memberCount":"4","unreadMessages":"0","unreadNotifications":"0","features":{"ownerId":null,"skuId":null,"premium":"0","locationUpdatesLeft":0,"priceMonth":"0","priceYear":"0","skuTier":null}},{"id":"xxxxxxxx","name":"Rob's Family","color":"f05929","type":"basic","createdAt":"1393782349","memberCount":"4","unreadMessages":"0","unreadNotifications":"0","features":{"ownerId":null,"skuId":null,"premium":"0","locationUpdatesLeft":0,"priceMonth":"0","priceYear":"0","skuTier":null}},{"id":"xxxxxxxx","name":"Leach Family","color":"7f26c2","type":"basic","createdAt":"1393785316","memberCount":"6","unreadMessages":"0","unreadNotifications":"0","features":{"ownerId":null,"skuId":null,"premium":"0","locationUpdatesLeft":0,"priceMonth":"0","priceYear":"0","skuTier":null}}]
Why don't I get the value reported in the console from the server method, in the client?
I have tried putting 'resolve' in various places, but I can't seem to figure out where to put it to turn the promise into the content on the client side.
In an experiment, I tried providing a callback method to getCircles
that does node.send([{payload: circles}])
and that ended up sending the circle data out of the node red node I'm working on. Then I tried just making it return circles, and I still get the unresolved promise object.
There's another method called getCircle in ../index.js and location.js uses that to extract member data, which works... Maybe I need to edit getCircles to build and return the data (a hash of circle IDs -> circle names) that I planned to build after receiving the circle data?