TypeError: Cannot read property 'exp' of undefined

TypeError: Cannot read property 'exp' of undefined

我在我的 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',那是因为这些是我的 discord 机器人的命令。

您的对象行似乎未定义。

将所有代码包裹在 else 花括号中:

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});
    }
})

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

试试这个,

  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});
  }