反应js mysql:TypeError:无法读取属性' affectedRows'

时间:2017-08-22 12:55:10

标签: mysql node.js node-modules

我添加distibuteur时出错,有人可以帮我解决此错误。

  

C:\用户\管理员\桌面\ PROJET-FALI \ node_modules \ MySQL的\ lib中\协议\ Parser.js:80       扔错了; //重新抛出非MySQL错误       TyepError:无法读取属性' affectedRows'在Query._callback

中未定义

=========================== code =================== =====

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。 感谢

2 个答案:

答案 0 :(得分:0)

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

hello how are you {d}o{g}? "d" I am "{g}oo{d}" "g" ha {g}oo{d}

答案 1 :(得分:0)

尽管这个问题很旧,但我遇到了同样的问题,我认为必须回答它。当我按如下方式放置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;