Javascript 是否在node.js中返回空值?
我是新来的Javascript 是否在node.js中返回空值?,javascript,node.js,Javascript,Node.js,我是新来的nodejs。这是我在nodejs文件中的代码。 我想将数据从nodejs发送到其他javascript使用json.stringify,但我的问题是我得到null值。。。 ----------------编辑----------------------- 我的代码是 function handler ( req, res ) { calldb(dr,ke,function(data){ console.log(data); //successf
nodejs
。这是我在nodejs
文件中的代码。
我想将数据从nodejs
发送到其他javascript
使用json.stringify
,但我的问题是我得到null值。。。
----------------编辑-----------------------
我的代码是
function handler ( req, res ) {
calldb(dr,ke,function(data){
console.log(data); //successfully return value from calldb
});
//i think my problem bellow...
res.write( JSON.stringify(data)); //send data to other but it's null value
res.end('\n');
}
function calldb(Dr,Ke,callback){
// Doing the database query
query = connection.query("select id,user from tabel"),
datachat = []; // this array will contain the result of our db query
query
.on('error', function(err) {
console.log( err );
})
.on('result', function( user ) {
datachat.push( user );
})
.on('end',function(){
if(connectionsArray.length) {
jsonStringx = JSON.stringify( datachat );
callback(jsonStringx); //send result query to handler
}
});
}
如何解决此问题?您将需要使用回调,直接返回数据只会返回
null
,因为当所有数据就绪后,会调用end
事件处理程序。尝试以下方法:
function handler ( req, res ) {
calldb(dr, ke, function(data){
console.log(data);
res.write( JSON.stringify(data));
res.end('\n');
});
}
function calldb(Dr,Ke, callback) {
var query = connection.query('SELECT id,userfrom tabel'),
datachat= []; // this array will contain the result of our db query
query
.on('error', function(err) {
console.log( err );
})
.on('result', function( user ) {
datachat.push( user );
})
.on('end',function() {
callback(datachat);
});
}
您将需要使用回调,直接返回数据只会返回
null
,因为当所有数据准备就绪后会调用end
事件处理程序。尝试以下方法:
function handler ( req, res ) {
calldb(dr, ke, function(data){
console.log(data);
res.write( JSON.stringify(data));
res.end('\n');
});
}
function calldb(Dr,Ke, callback) {
var query = connection.query('SELECT id,userfrom tabel'),
datachat= []; // this array will contain the result of our db query
query
.on('error', function(err) {
console.log( err );
})
.on('result', function( user ) {
datachat.push( user );
})
.on('end',function() {
callback(datachat);
});
}
问题是nodejs是异步的。它将执行res.write(JSON.stringify(data));在调用函数之前。您有两种选择:一种是避免回调:
.on('end',function(){
if(connectionsArray.length) {
jsonStringx = JSON.stringify( datachat );
res.write( JSON.stringify(data));
res.end('\n');
}
}
另一个在回调函数中有如下响应:
function boxold() {
box(function(data) {
res.write( JSON.stringify(data));
res.end('\n');
//console.log(data);
});
}
问题是nodejs是异步的。它将执行res.write(JSON.stringify(data));在调用函数之前。您有两种选择:一种是避免回调:
.on('end',function(){
if(connectionsArray.length) {
jsonStringx = JSON.stringify( datachat );
res.write( JSON.stringify(data));
res.end('\n');
}
}
另一个在回调函数中有如下响应:
function boxold() {
box(function(data) {
res.write( JSON.stringify(data));
res.end('\n');
//console.log(data);
});
}
谢谢..我遵循你的代码,但我得到错误“字符串不是函数”…如何修复?我的问题是修复的,但我在其他函数中发送值时再次得到空值…请参阅编辑(我更改代码)谢谢..我遵循你的代码,但我得到错误“字符串不是函数”…如何修复?我的问题是修复,但当我在其他函数中发送值时,我再次得到空值…请参见编辑(我更改代码)