2014-11-28 2 views
-1

nodejs가있는 웹 페이지를 관리하는 파일을 만들고 있는데 도움이 필요합니다.JSON 객체에 새 배열을 추가하는 데 도움이 필요합니다.

DB에서 수신 한 JSON 개체에 몇 가지 정보를 추가하고 있습니다. DB는 스테이지 정보를 유지하므로 스테이지 파일에 대한 정보를 링크해야합니다.

이것은 기능 코드입니다. 코드에 주석을 추가했습니다.

DBmodel.findOne({'key': 'server'}).exec(function (err, data) { 
    for (var i = 0; i < data.stage.length; i++) { 
     // this was successful. I added an array to check each files. 
     data.stage[i].packFileNameArray = data.stage[i].packFileName.split("/"); 
     data.stage[i].packVersionArray = data.stage[i].packVersion.split("/"); 

     // this is a problem. I will add file information for each file. 
     // I 
     for (var j = 0; j < data.stage[i].packFileNameArray.length; j++) { 
      fs.stat(dirPath + data.stage[i].packFileNameArray[j], function (err, fileStat) { 
       if (err) { 
        // this was first try. I thought the array will be automaticallt created. 
        data.stage[i].isExist[j] = 'NO'; 
        data.stage[i].mTime[j] = '0'; 
        data.stage[i].size[j] = '0'; 
       } else { 
        // this was second try. I tried push into each time. 
        data.stage[i].isExist.push('YES'); 
        data.stage[i].mTime.push(fileStat.mTime); 
        data.stage[i].size.push(fileStat.size); 
       } 
       console.log(data.stage[i].isExist[j]); 
       console.log(data.stage[i].mTime[j]); 
       console.log(data.stage[i].size[j]); 
      }); 
     } 

    } 

    }); 

추가 정보를 JSON 객체에 배열로 추가하는 방법을 알고 싶습니다.

감사합니다.

+0

에는 JSON 객체가 없습니다. 객체가 있고 JSON (특정 규칙에 따라 형식이 지정된 문자열)이 있습니다. 무엇을 추가하고 싶습니까? 결과로 원하는 것은 무엇입니까? – Amadan

+0

모델 스키마를 게시 할 수 있습니까? 아니면 원래 JSON이 이전과 같이 보이고 이후에 원하는 것을 게시 할 수 있습니까? – gabereal

답변

0
data.stage[i].push({ isExist: 'Yes'}); 

0

당신은 ((가) 존재하지 않는 것 같은) 당신이 그들에 데이터를 밀어 전에 배열을 만들 수 있습니다

DBmodel.findOne({'key': 'server'}).exec(function (err, data) { 
    for (var i = 0; i < data.stage.length; i++) { 
    // this was successful. I added an array to check each files. 
    data.stage[i].packFileNameArray = data.stage[i].packFileName.split("/"); 
    data.stage[i].packVersionArray = data.stage[i].packVersion.split("/"); 

    data.stage[i].isExist = []; 
    data.stage[i].mTime = []; 
    data.stage[i].size = []; 
    //... 
관련 문제