简体   繁体   中英

node.js and mysql how do I get 2 if have 1 in the database?

I want to add + 1 to the database and with that I would have 2 in the database if I had 1 in the case, I tried it but got 11, not 2

if (result4.length) {
  var sql1 = `UPDATE level SET xp = '${result4[0].xp + 1}' WHERE xp = '${result4[0].xp}'`;

  connection.query(sql1, function(err, result) {
    if (err) throw err;
  });
}

Here is the full source, just in case:

client.on("message", message => {
  if (message.author.bot) return;
  connection.query(`SELECT * FROM guildn WHERE id = '${message.guild.id}'`, function(err, result3) {
    if (err) {
      return console.log('Error1');
    }
    if (result3.length) {
      connection.query(`SELECT * FROM level WHERE id = '${message.author.id}'`, function(err, result4) {
        if (err) {
          return console.log('Error1');
        }
        if (!result4.length) {
          var sql = `INSERT INTO level (guild , id , nivel , xp) VALUES ('${message.guild.id}','${message.author.id}','0','1')`;
          connection.query(sql, function(err, result) {
            if (err) throw err;
            console.log("1 record inserted");
            return
          });
        }
        if (result4.length) {
          var sql1 = `UPDATE level SET xp = '${result4[0].xp + 1}' WHERE xp = '${result4[0].xp}'`;

          connection.query(sql1, function(err, result) {
            if (err) throw err;
          });
        }
      });
    }
  });
});

I used the google translator to make this post, so if I have any errors in the translation, I'm sorry

My suspicion would be that the result from the database xp is a string. This seems logical since you are setting it as a string in your query. In that case you would do this:

`UPDATE level SET xp = '${parseInt(result4[0].xp) + 1}' WHERE xp = '${result4[0].xp}'`;

This converts the result to a number and javascript will add 1 to it, rather than concatenating 1 to the string.

I'm guessing what's happening is that your XP data is stored in your database as a string so you end up trying to add a number to a string. In Javascript, the result in that situation is that you concatenate the number to the end of the string, which is why you end up with '11' instead of 2.

To resolve the issue without changing the database, you can turn the XP string into an integer using parseInt before adding 1 to it:

        var sql1 = `UPDATE level SET xp = '${ parseInt (result4[0].xp) + 1}' WHERE xp = '${result4[0].xp}'`;

If the data type of xp is a string then a little modification to your update call would suffice:

if (result4.length) {
  const query = `UPDATE level SET xp = '${Number(result4[0].xp) + 1}' WHERE xp = '${result4[0].xp}'`;

  connection.query(query, function(err, result) {
    if (err) throw err;
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM