在异步请求完成之前快速调用404

时间:2015-08-01 20:13:15

标签: javascript node.js asynchronous express async.js

我正在使用瀑布运行async.js的异步请求。瀑布的第二个函数调用nodejs https请求。但是,在请求完成之前,express表示调用404函数。为什么这样做以及如何在保持404功能的同时修复它。

请求代码:

router.post('/', auth.canWrite('Events'), function(req, res, next){
  var key = 'blahblah;
  req.body.creator = req.user._id;
  console.log('a');
  async.waterfall([
      function(callback){
        Location.findOne({name: req.body.location}, function(err, location){
          if(err) callback(err);

          if(location) callback(null, true, location.lat, location.lng);
          else callback(null, false, 0, 0);
          return;
        })
      },
      function(skip, lat, lng, callback){
        if(skip){
          console.log('skip geocode');
          callback(null, lat, lng);
          return;
        }
        else {
          if(!req.body.address){
            callback('No location found and no address given');
            return;
          }
          var location = req.body.address.toString();
          console.log('a');
          var request = http.get('https://maps.googleapis.com/maps/api/geocode/json?address=' + location + '&key=' + key, function(res){
            console.log(res.statusCode);
            console.log('b');
            var data = '';
            res.on('data', function (chunk){
                data += chunk;
                console.log('data');
            });
            res.on('end',function(){
              console.log('end');
                var obj = JSON.parse(data);
                if(obj.status==='OK'){
                  obj = obj.results[0].geometry.location;
                  console.log( obj );
                  callback(null, obj.lat, obj.lng);
                  return;
                }
                else{
                  callback(obj.status);
                  return;
                }
            })
          });
          request.on('error', function(error){
            console.log('c');
            callback(error);
            return;
          })
          console.log('d');
        }
      },
      function(lat, lng, callback){
        var event = new Event({
          title: req.body.title,
          location: req.body.location,
          date: req.body.date,
          lat: lat,
          lng: lng,
          description: req.body.description
        }).save(function(err) {
          console.log('saved');
          callback(err, 'done');
        });

 }   ], 
    function(err, results){
      if (err) { res.status(403).send({Error: err}); }
      res.status(200).send();
    });
})

404功能:

var controllers = glob.sync(config.root + '/app/controllers/*.js');
  controllers.forEach(function (controller) {
    require(controller)(app);
  });

  app.use(function (req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
  });

控制台返回文字:

a
a
d
POST /api/events 404 86.153 ms - 257
200
b
data
end
{ lat: 40.7313942, lng: -73.9881904 }
saved

更新

看起来auth.canWrite()中间件多次调用next()。

0 个答案:

没有答案