2016-10-04 95 views
-4

我试图将值返回给映射数组,该值的名称是latestDeploymentDate,但是,我收到解析错误,如标题中所列。我想返回最新部署的时间,我使用ta.ago函数,但是,由于这个原因,我收到了一个解析错误,我曾想过创建一个单独的数组,但似乎无法使其工作。它需要像这样返回,因为会有多个时间实例,需要返回输出。节点解析错误:返回时出现意外的令牌

const express = require('express')(); 
    const request = require('request'); 
    const moment = require('moment'); 
    const ta = require('time-ago')(); 

    const webApplications = require('./XXX.json'); 



    express.listen(3000, function() { 

     console.log('XXX...'); 
     express.get('/', function(req, res) { 
     res.json(webApplications.map((item, latestDeploymentDate) => { 
      res.setHeader('Access-Control-Allow-Origin', '*'); 
      res.setHeader('Access-Control-Allow-Headers', true); 
      const newrequest = { 
      url: `XXX`, 
      headers: { 
       'X-Api-Key': 'XXX' 
      } 
      } 
      const gitlabRequest = { 
      url: `XXX`, 
      headers: { 
       'PRIVATE-TOKEN': 'XXX' 
      } 
      } 
      request(newrequest,(err, resp, body) => { 
      const data = JSON.parse(body); 
      const latestDeployment = data.deployments[0]; 
      latestDeploymentDate = new Date(latestDeployment.timestamp); 

      // console.log(latestDeploymentDate, laz1testDeployment); 
      // res.json({latestDepoloyment: latestDeployment}); 
      // return; 

      request(gitlabRequest, (err, resp, body) => { 
       const gitlabData = JSON.parse(body); 
       const currentTag = latestDeployment.description; 
       var currentIndex; 

      console.log(`${item.appName}` + ' '); 
      console.log(`Last deployed: ${latestDeployment.description} 
       on ${moment(latestDeploymentDate).format('YYYY-MM-DD')}`); 

       gitlabData.find((item, index) => { 
       currentIndex = index; 
       return item.name == currentTag 
       }); 
       if (currentIndex > 3) { 
${ta.ago(latestDeploymentDate)}`); 
       console.log(`This ${currentIndex} was released ${ta.ago(latestDeploymentDate)}`); 
       var deployedDate = ta.ago(latestDeploymentDate); 
       } else { 
${ta.ago(latestDeploymentDate)}`); 
       var deployedDate = ta.ago(latestDeploymentDate); 
       console.log(`This ${currentIndex} was released ${ta.ago(latestDeploymentDate)}`); 
       } 
       // res.end(); 
      }) 
      }) 
      return {appName: item.appName, ta.ago(latestDeploymentDate)}; 
     })); 
     }); 
    }) 
+0

你可以张贴错误堆栈跟踪? –

+0

/Users/jba71/last-app-web/server-copy-new.js:66 return {appName:item.appName,ta.ago(latestDeploymentDate)}; ^ SyntaxError:意外的标记。 在exports.runInThisContext(vm.js:53:16) 在Module._compile(module.js:387:25) 在Object.Module._extensions..js(module.js:422:10) 在模块(module.js:357:32) at Function.Module._load(module.js:314:12) at Function.Module.runMain(module.js:447:10) at startup(node.js :148:18) at node.js:405:3 – DaveDavidson

回答

1

缺少JSON键返回对象:

return {appName: item.appName, MYKEY: ta.ago(latestDeploymentDate)};