samedi 2 avril 2016

query results are undefined

exports.save = function(req, res) {

connection.query('INSERT INTO student_details(name, course, units, grades, gwa) VALUES(?, ?, ?, ?, ?)',[req.body.name,req.body.course,req.body.units,req.body.grades,req.body.gwa], function(err, row) {
    if(err) res.send('Error in query');
    selectOne(row.insertId, function(newRow){
        if(err) res.status(554).send('Error in query');
        if(newRow == null){
            res.send(552, {message: 'Student Details ('+row.insertId+') was not created.'});
        } else{
            res.status(200).send(newRow);
        }
    });
});

}

var selectOne = function(id, callback){

connection.query('SELECT * FROM student_details WHERE id=? LIMIT 1', [id], function(err, rows){
    if(err) return err;
    if(rows.length != 0){
        callback(rows[0]);
    }else{
        callback(null);
    }
});

}

I'm having an error performing the query above. It's saying this error " Uncaught TypeError: Cannot read property 'insertId' of undefined".

I've been trying to figure this out for hours now, and my hunch is that it's somewhat related to asynchronous values but I can't figure out how to fix it. Someone help me?

Aucun commentaire:

Enregistrer un commentaire