Thursday, 25 May 2017

Slack Clear MemoryDataStore

I use the following code to setup the RtmClient and dataStore for Slack in Node.js.

var rtm = new RtmClient(bot_token, {
  // Sets the level of logging we require
  logLevel: 'error',
  // Initialise a data store for our client, this will load additional helper functions for the storing and retrieval of data
  dataStore: new MemoryDataStore()
});

Then I have the following function to get the DM for the channel.

function getUserDM(channel, callback) {
    if (connectionopen) {
        var user = rtm.dataStore.getDMByName(channel);
        console.log(user);
        callback(user);
    } else {
        console.log("Error connection open is false");
        callback("Error connection open is false");
    }
}

Problem is if I call this function then for whatever reason the DM updates and I call the function again user equals the same thing. I have to close the server and restart the node process and recall it in order for it to update.

I used the following code to try to clear the data store before calling getUserDM but that just results in user being undefined.

function clearDataStore() {
    rtm.dataStore.clear();
}

How can I get the most up to date data without restarting the server and without it returning undefined?



via Charlie Fish

No comments:

Post a Comment