无法添加到Array

时间:2017-04-20 15:07:09

标签: javascript node.js express

我正在尝试运行以下代码,但每次运行它时,我都会发现markerObj数组为空。

var express = require('express');
var router = express.Router();
var ExifImage = require('exif').ExifImage;
var fs = require('fs');
var path = require('path');

function decimalDegrees(gps, fileName) {
  latitude = gps['GPSLatitude'];
  longitude = gps['GPSLongitude'];
  imageLink = "http://localhost:3000/tmp/" + fileName;
  latitude = latitude[0] + latitude[1] / 60 + latitude[2] / 3600;
  longitude = longitude[0] + longitude[1] / 60 + longitude[2] / 3600;
  console.log(latitude);
  console.log(longitude);
  var markerObject = {
    latitude: latitude,
    longitude: longitude,
    imageLink: imageLink
  };
  return markerObject;
}

router.get('/', function(req, res, next) {

  res.sendFile('map.html', {
    root: 'C:\\Users\\Pranav\\WebstormProjects\\majorProject\\views\\'
  });

});

router.get('/map', function(req, res, next) {
  var markerObj = [];
  fs.readdir("C:\\Users\\Pranav\\WebstormProjects\\majorProject\\tmp", function(err, files) {
    if (err) {
      console.error("Could not list the directory.", err);
      process.exit(1);
    }
    files.forEach(function(file, index) {
      new ExifImage({
        image: path.join("C:\\Users\\Pranav\\WebstormProjects\\majorProject\\tmp", file)
      }, function(error, exifData) {
        if (error)
          console.log('Error: ' + error.message);
        pothole = decimalDegrees(exifData["gps"], file);
        markerObj.push(pothole); // Do something with your data!
      });
    });
    console.log(markerObj);
    res.render('map', {
      markers: markerObj
    });
  });
});

知道出了什么问题吗?

0 个答案:

没有答案