2015-04-28 10 views
6

Sono nuovo nel nodo js, ​​ora sto cercando di impostare un valore di ritorno della query select in mysql usando il nodo js .... Sto usando node-mysql pacchetto ...non può restituire un valore di query select usando mysql e node js

codice di esempio

var mysql = require('mysql'); 
var connection = mysql.createConnection({ 
    host : "localhost", 
    user : "root", 
    password: "root123", 
    database: "testdb" 
}); 

var retValue = undefined; 

var query = connection.query('SELECT * FROM tblData;'); 
query 
    .on('error', function(err) { 
     // Handle error, an 'end' event will be emitted after this as well 
    }) 
    .on('fields', function(fields) { 
     // the field packets for the rows to follow 
    }) 
    .on('result', function(row) { 
     // Pausing the connnection is useful if your processing involves I/O 
     connection.pause(); 
     processRow(row, function() { 
      retValue = row; 
     }); 
    }) 
    .on('end', function(row) { 

    }); 

    connection.end(); 

    function processRow(rows) 
    { 
     retValue = rows; 
    } 

    console.log(retValue); 

retValue è sempre indefinito. So che è una chiamata asincrona. Qualcuno mi dice come impostare il valore per questa variabile.

Grazie Deepak

+0

Perché si desidera assegnare 'retValue' in' processRow' e nella sua richiamata? – Lewis

+0

appena provato .... ma non riesco a impostare il risultato su quella variabile .... –

+0

http://stackoverflow.com/questions/5964988/how-to-get-the-results-from-nodejs-using -mysql-package possibile aiuto – vanadium23

risposta

0

Il retvalue sarà disponibile solo in ambito, che la messa in risultato secion. Così si può fare qualcosa di simile per registrare il risultato:

var connection = mysql.createConnection({ 
    host : "localhost", 
    user : "root", 
    password: "root123", 
    database: "testdb" 
}); 

var retValue = undefined; 

var query = connection.query('SELECT * FROM tblData;', function(err, results, fields) { 
    console.log(results); 
    // Here continue logic you needed with results from table 
}); 
+0

non riesco a ottenere il risultato fuori ambito ... voglio assegnare e restituire il valore come questo fucntion() getResults {... ... var retValue = undefined; var query = connection.query ('SELECT * FROM tblData;', function (err, results, fields) {retValue = results;}); return retValue}; var ret = getResults(); console.log (ret); –

+0

@DeepakSamuel node.js funzionano in ambiti e callback maner. È la sua natura. Sei sicuro di aver bisogno di implementare il codice in questo modo? Puoi provare a sincronizzare vars tramite l'interfaccia di promessa. – vanadium23

+0

oh ... ok grazie per le informazioni .... puoi mettere qualche esempio di prmoise? –

0

Spostare il console.log all'interno function processRow(rows) in modo che appaia come

function processRow(rows) 
{ 
    retValue = rows; 
    console.log(retValue); 
} 

anche cambiare

processRow(row, function() { 
    retValue = row; 
}); 

Per

processRow(row); 

Il secondo parametro non è stato utilizzato.

+0

non riesco a ottenere il risultato fuori ambito ... voglio assegnare e restituire il valore come questo fucntion() getResults {... ... var retValue = undefined; var query = connection.query ('SELECT * FROM tblData;', function (err, results, fields) {retValue = results;}); return retValue}; var ret = getResults(); console.log (ret); –

+0

non funziona patrick .... il log della console mostra sempre i risultati .... ma non riesco a ottenere il risultato dalla funzione –

+0

@DeepakSamuel non è possibile ottenere il risultato dalla funzione. È così che funzionano i callback asincroni. Vengono eseguiti _after_ viene eseguita la parte esterna, anche la parte esterna dopo il blocco funzione. –

0

non riesco a ottenere il risultato fuori portata ... voglio assegnare e restituire il valore come questo

fucntion() {getResults ... ... var retvalue = undefined;

var query = connection.query ('SELECT * FROM tblData;', funzione (err, risultati, campi) { retValue = risultati; });

return retValue };

var ret = getResults(); console.log (ret);

+0

Questa non è una risposta. –

1

Poiché la query del database è un'opinione asincrona, la variabile retValue non è stata ancora impostata nel momento in cui si chiama console.log(retValue).

var retValue; 

var query = connection.query('SELECT * FROM tblData;'); 
query 
    .on('error', function(err) { 
     // Handle error, an 'end' event will be emitted after this as well 
    }) 
    .on('fields', function(fields) { 
     // the field packets for the rows to follow 
    }) 
    .on('result', function(row) { 
     // Pausing the connnection is useful if your processing involves I/O 
     connection.pause(); 
     processRow(row); 
     console.log(retValue); //retValue is now set 
    }) 
    .on('end', function(row) { 

    }); 

    connection.end(); 

    function processRow(rows) 
    { 
     retValue = rows; 
    } 

    console.log(retValue); // undefined, retValue has not been set yet