javascripthubot

If I set a value in robot.brain too early, it gets overwritten by hubot-redis-brain


If I initialize a property in robot.brain in the body of my script's module.exports, it doesn't work (see code below). If I initialize it during response, it works. Is my hypothesis that it gets overwritten by hubot-redis-brain correct? How do I fix it in a nice way?

module.exports = (robot) => {
    robot.logger.debug("Setting the fucking property");
    robot.brain.set("stringproperty", "stringvalue");
    robot.logger.debug("SET!");
    // logs these two entries before 'INFO hubot-redis-brain: Data for hubot brain retrieved from Redis'

    const respondAndLog = (res, message) => {
        robot.logger.debug("Responding: " + message);
        res.reply(message);
    };

    robot.respond(/get_stringproperty/, (res) => {
        respondAndLog(res, `${robot.brain.get("stringproperty")}`);
        // prints null. WTF?
    });

    robot.respond(/get_laterinitializedproperty/, (res) => {
        robot.brain.set("laterinitializedproperty", "laterinitializedvalue");
        respondAndLog(res, `${robot.brain.get("laterinitializedproperty")}`);
    // prints laterinitializedproperty, works OK
    });
};

Solution

  • hubot-redis-brain makes robot.brain emit a "connected" event when the data is loaded from redis or initialized, see [https://github.com/hubotio/hubot-redis-brain/blob/487dd4a9641f35ffb5ae18fb5e1b09e8114c4b70/src/redis-brain.js#L55](see lines 55 and 59). So this is how it should be fixed:

    robot.brain.on("connected", () => {
        robot.logger.debug("Setting the fucking property");
        robot.brain.set("stringproperty", "stringvalue");
        robot.logger.debug("SET!");
    });