react js mysql:TypeError:无法读取属性“affectedRows”

xoshrz7s  于 2023-03-22  发布在  Mysql
关注(0)|答案(3)|浏览(284)

我有一个错误添加distributeur,有人可以帮助我请这个错误。
c:\users\admin\Desktop\projet-fali\node_modules\mysql\lib\protocol\Parser.js:80 throw err;//重新抛出非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。谢谢

ubof19bj

ubof19bj1#

只要看看你如何处理每个查询都会给予你一个can error或者一个结果,就像javascript处理error一样,你在回调中的第一个参数总是会给你一个can error,第二个是结果。所以你可以使用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([]);
                    }
                });
            }
        });
km0tfn4u

km0tfn4u2#

虽然这个问题是旧的,我遇到了同样的问题,我觉得这是需要回答它。它为我工作时,我把参数的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;
3phpmpom

3phpmpom3#

app.get('/addstudent',(req,res)=>{
    // res.send(req.query);
    const {name,phone,email,gender} = req.query
    let qry = "select * from test where email=? and phoneno=?";
    mysql.query(qry,[email,phone],(err,results)=>{
        if(err)
        throw err
        else{
            if(results.length > 0){

            }else{
                let qry2 = "insert into test values(?,?,?,?)";
                mysql.query(qry2,[name,phone,email,gender],(err,results)=>{
                    if(results.affectedRows>0){
                        res.render("add",{mesg:true})
                    }
                })
            }
        }
    })
})

error : Cannot read properties of unde`enter code here`fined (reading 'affectedRows')

相关问题