节点分析错误:返回时出现意外的令牌

时间:2016-10-04 08:21:06

标签: javascript json node.js parsing mapping

我正在尝试将值返回到映射数组,此值的名称是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)};
        }));
      });
    })

1 个答案:

答案 0 :(得分:1)

在返回对象中缺少json键:

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