简体   繁体   English

函数返回undefined时使用

[英]function returning undefined when using

i have a function that connect to MsSQL database to get some data and build a text statement , here it's : 我有一个连接到MsSQL数据库的函数来获取一些数据并构建一个文本语句,这里是:

function getActions(issue_id, callback){
    var action = '';
    var conn2 = new sql.Connection(config, function(err) {
        if(err){
            showNotification('error connecting for selecting actions for ALL issues: ' + err.message, 'danger', 'glyphicon glyphicon-tasks');
        } else {
            var request = new sql.Request(conn2);
            request
            .input('issue_id', sql.Int,issue_id)
            .query('SELECT [date], [description] FROM [actions] WHERE [issue_id] = @issue_id')
            .then(function(data2) {
                action ='<tr>'+
                                '<td style="vertical-align: top;" class="bold">action:</td>'+
                                '<td>'+
                                '<table>';
                data2.forEach(function(data21){
                        action +='<tr>'+
                                    '<td>'+data21.date+'</td>'+
                                    '<td>'+data21.description+'</td>'+
                                    '</tr>';

                });
                action += '</table>'+
                            '</td>'+
                            '</tr>'+
                            '</tbody>'+
                            '</table>';
            console.log(action);
            callback(null, action);
        }).catch(function(error) {
            showNotification('Error on selecting actions for ALL issues:' + error.message, 'danger', 'glyphicon glyphicon-tasks');
        });

    }

});    
}

but when i use the function in another place in my code it return undefined instead of the text statement , i make a console.log(action) before the return statement it it return the correct text ..... i did't get it why it return undefined when using the function 但是当我在我的代码中的另一个地方使用该函数它返回undefined而不是text语句时,我在return语句之前创建一个console.log(action)它返回正确的文本.....我没有得到它为什么在使用函数时返回undefined

UPDATE when i used the function from Aruna i'm using the function like this: 更新当我使用Aruna的功能我正在使用这样的功能:

for(let b=0;b<id.length;b++){
    getActions(id[b],function(err, action) {
        arrAction[b] = action;
    });
 }

As you connect and query MySQL database asynchronously, you can't expect the data returned synchronously. 当您异步连接和查询MySQL数据库时,您不能指望同步返回数据。

You should use 'callback' to handle this. 您应该使用'callback'来处理这个问题。

So your code can be like this, 所以你的代码可以是这样的,

function getActions(issue_id, callback){
var action = '';
var conn2 = new sql.Connection(config, function(err) {
    if(err){
        showNotification('error connecting for selecting actions for ALL issues: ' + err.message, 'danger', 'glyphicon glyphicon-tasks');
    } else {
        var request = new sql.Request(conn2);
        request
        .input('issue_id', sql.Int,issue_id)
        .query('SELECT [date], [description] FROM [actions] WHERE [issue_id] = @issue_id')
        .then(function(data2) {
            action ='<tr>'+
                            '<td style="vertical-align: top;" class="bold">action:</td>'+
                            '<td>'+
                            '<table>';
            data2.forEach(function(data21){
                action +='<tr>'+
                                '<td>'+data21.date+'</td>'+
                                '<td>'+data21.description+'</td>'+
                                '</tr>';

            });
            action += '</table>'+
                            '</td>'+
                            '</tr>'+
                            '</tbody>'+
                            '</table>';
            console.log(action);
            callback(null, action);
        }).catch(function(error) {
            showNotification('Error on selecting actions for ALL issues:' + error.message, 'danger', 'glyphicon glyphicon-tasks');
        });

    }

});    
}

And you should call the same like this, 你应该像这样打电话,

var issue_id = 12;
getActions(issue_id, function(err, action) {
   // to do
});

Also, change your for loop like this, as you called asynchronous function inside the loop, the for loop variable "b" will get changed before the response comes from the function 另外,像这样改变你的for循环,就像在循环中调用异步函数一样,for循环变量“b”将在响应来自函数之前被更改

var arrAction = [];
for(let b=0;b<id.length;b++){
    getActions(id[b],function(err, action) {
        arrAction[arrAction.length] = action;
    });
 }

getActions does not return anything. getActions不会返回任何内容。

return action is called within an inner function, something to do with the sql.Connection class. 在内部函数中调用return action ,与sql.Connection类有关。 Perhaps you should return conn2; 也许你应该return conn2; at the end of getActions ? getActions结束时? It is hard to say without context on what you are doing. 没有关于你在做什么的背景很难说。

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

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