简体   繁体   English

无法在express js中调用未定义的方法“发送”(响应未定义)

[英]Cannot call method 'send' of undefined(response is undefined) in express js

I have tried to pass a variable from my index.html to the database(maildata.js) through app.js(server) and get the corresponding data I am able to get the data from the database but couldnt send that back to the server(app.js) 我试图通过app.js(server)将变量从index.html传递到数据库(maildata.js)并获取相应的数据,我能够从数据库中获取数据,但无法将其发送回服务器(app.js)

app.js app.js

var express = require('express');
var maildata= require('./maildata');
var app = express();
app.configure(function(){
app.use(express.bodyParser());
});

app.get('/', function(request, response){
response.sendfile(__dirname + '/mailbox.html');
});
app.post('/mailboxpost',function(request, response) {
    var input=request.query.search;     
var result=maildata.getMailData(input); 
response.send(result);
response.end();
});

app.listen(8888);
console.log('Server is running on port 8888'); 

maildata.js maildata.js

    exports.getMailData=function(data,response) {

    var stop_name= data;    
    connection.query("select stop_name,stop_comment from stoplist where stop_name=        '"+stop_name+"' limit 1",function(err, rows) {    

    if (err) {
        console.log('error in fetching  ' + err);   
    }
    else{    
        var jsonString1= JSON.stringify(rows);
    connection.query("select mailbox_sequence_no from stoplist where stop_name= '"+stop_name+"'",function(err, rows) {  

    if (err) {
        console.log('error in fetching  ' + err);   
    }
    else{    
        var jsonString2 = JSON.stringify(rows);
         connection.query("select party_head from stoplist where stop_name= '"+stop_name+"'", function(err, rows) { 
     if (err) {
        console.log('error in fetching  ' + err);   
     }
     else{  
        var jsonString3 = JSON.stringify(rows);
        var result=jsonString1+'/'+jsonString2+'/'+jsonString3;  
        response.send(result);    
      }    
    });
   }    
  });    
 }    
});    
}

Thanks in Advance 提前致谢

调用函数时如何发送响应?

var result=maildata.getMailData(input); // something missing here

Your getMailData function expects two arguments: 您的getMailData函数需要两个参数:

exports.getMailData=function(data,response) { ... }

but you give it only one: 但您只给它一个:

var result=maildata.getMailData(input); 

Which makes the value of the response argument undefined . 这使得response参数的值undefined

Here is what you should do: 这是您应该做的:

app.post('/mailboxpost',function(request, response) {
  var input=request.query.search;     
  maildata.getMailData(input, response); 
});

and let maildata.getMailData handle the response sending, as you did in response.send(result); 并让maildata.getMailData处理响应发送,就像您在response.send(result);所做的一样response.send(result);

I have used asynchronous callback method in my app.js. 我在我的app.js中使用了异步回调方法。

I got the result 我得到了结果

var result=maildata.getMailData(input,response,function(data){
response.send(data);
response.end();
});

Thanks all 谢谢大家

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM