NodeJS:回调不是一个函数

时间:2018-05-15 08:05:21

标签: node.js express callback

我已经检查了其他问题但没有形成我的答案。换句话说:没有答案可行。

我有两个模块:app和逻辑。应用程序将回调传递给逻辑,逻辑然后调用。但是我得到错误" cb不是函数"。这就是它,没有更多的信息。

这是构成回调的app.js中的函数:

ajaxStart

这是logic.js中调用回调的函数:

app.get('/assignments/open/:student', function (request, response) {
  var callback = function(mail, jobtitle) {
    response.status(200).json(logic.getOpenAssignments(request.params.student));
  }

  logic.examine(request, callback);
});

任何解决方案?提前谢谢!

编辑:

以下代码无效:

examine: function (request, cb) {
        var options = {
            host: 'graph.microsoft.com',
            path: '/v1.0/me',
            method: 'GET',
            headers: {
                'Content-Type': 'application/json',
                'Authorization': request.get('Authorization')
            }
        };

        var req = http.get(options, function(res) {
            res.on('data', function(chunk) {
                chunk = JSON.parse(chunk);
                mail = chunk['mail'];
                jobtitle = chunk["jobTitle"];
                cb(mail, jobtitle);
            })
          });
    },

以下代码也不起作用,这是否意味着范围不是问题?

examine: function (request, cb) {
        var options = {
            host: 'graph.microsoft.com',
            path: '/v1.0/me',
            method: 'GET',
            headers: {
                'Content-Type': 'application/json',
                'Authorization': request.get('Authorization')
            }
        };

        var req = http.get(options, res => {
            res.on('data', chunk => {
                chunk = JSON.parse(chunk);
                mail = chunk['mail'];
                jobtitle = chunk["jobTitle"];
                cb(mail, jobtitle);
            })
          });
    },

2 个答案:

答案 0 :(得分:0)

如果您正在使用ES6,请尝试使用箭头功能将范围传递给http.get的回调函数:

var req = http.get(options, res => {
    res.on('data', chunk => {
        chunk = JSON.parse(chunk);
        mail = chunk['mail'];
        jobtitle = chunk["jobTitle"];
        cb(mail, jobtitle);
    })
});

或者你也可以将cb传递给你的回调函数:

var req = http.get(options, function(res, cb) {
    res.on('data', function(chunk, cb) {
        chunk = JSON.parse(chunk);
        mail = chunk['mail'];
        jobtitle = chunk["jobTitle"];
        cb(mail, jobtitle);
    })
});

答案 1 :(得分:0)

也许问题是,您不是在等待数据块处理。请尝试以下代码段。添加了一种将http.get转换为promises(非常常见)的方法,这可以避免传递所有混乱的回调。

//put this somewhere inside where examine is located.
logic.httpGet = options => {
  return new Promise((resolve, reject) => {
    http.get(options, res => {
      res.setEncoding('utf8');
      let body = ''; 
      res.on('data', chunk => body += chunk);
      res.on('end', () => resolve(body));
    }).on('error', reject);
  });
};

//convert your route like this
app.get('/assignments/open/:student', function (request, response) {
  cont options = {
        host: 'graph.microsoft.com',
        path: '/v1.0/me',
        method: 'GET',
        headers: {
            'Content-Type': 'application/json',
            'Authorization': request.get('Authorization')
        }
    };
  return logic
          .httpGet(options)
          .then((data)=>{
             const data =JSON.parse(data);
             //i don't see where you use the processed data but do whatever you want below here
           repsonse.status(200).json(logic.getOpenAssignments(request.params.student))
          })
})