【问题标题】:react js mysql : TypeError: Cannot read property 'affectedRows'react js mysql:TypeError:无法读取属性'affectedRows'
【发布时间】:2018-01-30 18:40:15
【问题描述】:

我有一个添加分销商的错误,有人可以帮我解决这个错误。

c:\users\admin\Desktop\projet-fali\node_modules\mysql\lib\protocol\Parser.js:80 抛出错误; //重新抛出非MySQL错误 TyepError:无法读取 Query._callback 处未定义的属性“affectedRows”

============================代码================== =====

var express = require('express');
var router = express.Router();

//add new distributeur
router.post('/', function(req, res, next) {
    pool.getConnection(function(err, connection) {
        var postBody = req.body;
        var nom = postBody.nom;
        var prenom = postBody.prenom;
        var societe = postBody.societe;
        var adresse = postBody.adresse;
        var siret = postBody.siret;
        var email = postBody.email;
        var tel_fixe = postBody.tel_fixe;
        var tel_mob = postBody.tel_mob;
        var demande= "";
        var code =2;
        connection.query("INSERT INTO admin (nom, prenom, email, tel_fixe, adresse, nsiret, nom_entreprise, demande, code, tel_mob) VALUES ('" + nom + "','" + prenom + "','" + email + "','" + tel_fixe + "','" + adresse + "','" + siret + "','" + societe + "','" + demande + "','" + code + "','" + tel_mob + "')", function(err, rows) {
            if (rows.affectedRows) { // error is in this lign
                connection.query("SELECT * FROM admin WHERE  id='" + rows.insertId + "' LIMIT 1", function(err, rows) {
                    if (!err && rows.length > 0) {
                        res.json(rows[0]);
                    } else {
                        res.json([]);
                    }
                });
            }
        });
    });
});
module.exports = router;

我想学习如何处理 mysql 错误,因为我的应用程序需要 mysql。 谢谢

【问题讨论】:

    标签: mysql node.js node-modules


    【解决方案1】:

    看看你如何处理每个查询都会给你错误或结果,因为 javascript 首先处理错误,回调中的第一个参数总是给你错误,第二个结果。所以你可以使用 if else 条件来处理它。

     connection.query("INSERT INTO admin (nom, prenom, email, tel_fixe, adresse, nsiret, nom_entreprise, demande, code, tel_mob) VALUES ('" + nom + "','" + prenom + "','" + email + "','" + tel_fixe + "','" + adresse + "','" + siret + "','" + societe + "','" + demande + "','" + code + "','" + tel_mob + "')", function(err, rows) {
                if (err) {
    console.log("===============err====================",err) // error is in this line
    }
    else{
                    connection.query("SELECT * FROM admin WHERE  id='" + rows.insertId + "' LIMIT 1", function(err, rows) {
                        if (!err && rows.length > 0) {
                            res.json(rows[0]);
                        } else {
                            res.json([]);
                        }
                    });
                }
            });
    

    【讨论】:

    • 当我更改代码时,它会显示此错误。错误:er_truncated_wrong_value_for_field:不正确的整数值:第 1 行的列“ecranlap”的“未定义”反应 js
    • 只是数据库中的数据类型,您似乎正在将字符串输入到某个数据类型为整数的值。
    【解决方案2】:

    虽然这个问题很老,但我遇到了同样的问题,我觉得有必要回答它。当我将 sql 查询的参数如下放置时,它对我有用,这很清楚,并且在将它们添加到查询时需要方括号。缺少方括号会给出错误而不是 rows.affectedRows.Refer here

    router.post('/', function(req, res, next) {
        pool.getConnection(function(err, connection) {
            var postBody = req.body;
            var params = [[postBody.nom,postBody.prenom,postBody.societ, postBody.adresse,postBody.siret,postBody.email,postBody.tel_fixe,ostBody.tel_mob,"",2]];
            connection.query("INSERT INTO admin (nom, prenom, email, tel_fixe, adresse, nsiret, nom_entreprise, demande, code, tel_mob) VALUES ?", [params], function(err, rows) {
                if (rows.affectedRows > 0) {
                    connection.query("SELECT * FROM admin WHERE  id='" + rows.insertId + "' LIMIT 1", function(err, rows) {
                        if (!err && rows.length > 0) {
                            res.json(rows[0]);
                        } else {
                            res.json([]);
                        }
                    });
                }
            });
        });
    });
    module.exports = router;
    

    【讨论】:

      猜你喜欢
      • 2023-02-17
      • 1970-01-01
      • 2022-11-17
      • 2016-08-06
      • 2021-07-12
      • 1970-01-01
      • 1970-01-01
      • 2020-12-24
      • 2020-08-21
      相关资源
      最近更新 更多