TypeError:无法读取undefined的属性'exp'

时间:2017-08-02 08:58:38

标签: javascript sqlite discord

我在我的javascript代码中使用sqlite作为数据库,无论我尝试什么,它总是在那里保留这个错误:

sql.get(`SELECT * FROM users WHERE userId ="${member.user.id}"`).then(row => {
          if (!row) sql.run("INSERT INTO users (userId, level, exp) VALUES (?, ?, ?)", [member.user.id, 1, 0]);
          var profile = new Discord.RichEmbed()
          .setColor(0x0000FF)
          .setTitle(member.user.username + "'s profile")
          .setThumbnail(member.user.avatarURL)
          .setDescription("Status: " + member.user.presence.status)
          .addField("Stats","**Level** " + row.level+"\n"+row.exp+"/"+row.level*10)
          msg.reply("here is "+member.user.username+"'s profile:",{embed:profile});
        })

如果你没有像'msg.reply'那样低估一些,那是因为那些是我的不和谐机器人的命令。

2 个答案:

答案 0 :(得分:3)

看起来您的对象行未定义。

将所有代码包装在其他大括号内:

sql.get(`SELECT * FROM users WHERE userId ="${member.user.id}"`).then(row => {
      if (!row) 
        sql.run("INSERT INTO users (userId, level, exp) VALUES (?, ?, ?)", [member.user.id, 1, 0]);
      else {
        var profile = new Discord.RichEmbed()
          .setColor(0x0000FF)
          .setTitle(member.user.username + "'s profile")
          .setThumbnail(member.user.avatarURL)
          .setDescription("Status: " + member.user.presence.status)
          .addField("Stats","**Level** " + row.level+"\n"+row.exp+"/"+row.level*10)
        msg.reply("here is "+member.user.username+"'s profile:",{embed:profile});
    }
})

答案 1 :(得分:0)

在您的代码中,如果行不存在则插入行,但始终对其进行处理。 如果没有定义,那么你应该插入它,然后再次获取行,现在可以处理它。

试试这个,

  sql.get(`SELECT * FROM users WHERE userId ="${member.user.id}"`).then(row => {
    if(row) {
           reply(row, member);
    } else {
    sql.run("INSERT INTO users (userId, level, exp) VALUES (?, ?, ?)", [member.user.id, 1, 0]).then(row => {
            reply(row, member);
    })
    }
  })

  const reply = (row, member) => {
               var profile = new Discord.RichEmbed()
          .setColor(0x0000FF)
          .setTitle(member.user.username + "'s profile")
          .setThumbnail(member.user.avatarURL)
          .setDescription("Status: " + member.user.presence.status)
          .addField("Stats","**Level** " + row.level+"\n"+row.exp+"/"+row.level*10)
          msg.reply("here is "+member.user.username+"'s profile:",{embed:profile});
  }