PaulAsjes-zz / BuildingBots

Source code for the book Building Bots
1 stars 2 forks source link

Redis Problems. #2

Open aisflat439 opened 7 years ago

aisflat439 commented 7 years ago

Hi Paul, When I run the code for the todo bot with the following package.json:

{
  "name": "to-do-bot",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@slack/client": "^3.8.1",
    "redis": "^2.6.5"
  }
}

i get a Error ReplyError: WRONGTYPE Operation agianst a key holding teh wrong kind of value from Redis. Concerned that my typing may have been to blame I copied the raw files of bot.js and index.js. I then added:

console.log(name);
console.log(task);
console.log(typeof name);
console.log(typeof task);

around lines ~275 to verify that I was indeed adding strings for the Key Value pairing in Redis. I'm not really sure about next steps but wanted to give you the option to evaluate yourself if you think it's appropriate. I suppose that the difference in our Redis versions could be at fault here. I tend not to copy code and instead to type it as that improves my learning process so this could explain the problem here.

I have no experience with Redis so it's possible that my experience is unique to me. I found this chapter frustrating and demotivating, not because of poor writing or examples, but because I felt lost around Redis. Off to the NLP section of the book! Which I've enjoyed, depsite my personal struggles around Redis.

0xpetersatoshi commented 7 years ago

@aisflat439 did you ever figure this out?

I encountered the same issue so I looked up some examples of how to use the client.sadd() method and found this article using an example. In the example, it shows that the proper way to use the method is as follows:

client.sadd(['tags', 'angularjs', 'backbonejs', 'emberjs'], function(err, reply) {
    console.log(reply); // 3
});

Which is contrary to what the book has:

function addTask(name, task, channel) {

  (...)

  client.sadd(name, task);

  (...)

}

I ran the exact example from the article on the command line and it works just fine. Using the method from the book on the command line gave me the exact error you got.

So seeing as this looks like an error in the book, I modified the code as follows for a quick test:

bot.respondTo('todo', (message, channel, user) => {

  let args = getArgs(message.text);

  switch(args[0]) {
    case 'add':
      //addTask(user.name, args.slice(1).join(' '), channel);
      client.sadd([user.name, args.slice(1).join(' ')], (err, reply) => {
      console.log('adding: ' + reply);
      });
      break;

    default:
      //showTodos(user.name, channel);
      client.smembers(user.name, (err, reply) => {
        console.log('members: ' + reply)

}, true);

and then i ran todo add something interesting followed by todo which printed the following to the console:

adding: undefined
members: undefined

I am stuck on this now as I don't know why this is not working.

@PaulAsjes maybe you can enlighten us on the sorcery you performed to make this work as it looks like the book and github code are causing errors.

aisflat439 commented 7 years ago

@pbegle I never resolved my problem. I ran some similar experiments. I've since been distracted by other projects and have left Slack bots to the side. You don't need to complete the redis section to move foward however. That is what I chose to do.