2016-07-01 57 views
1

我正在嘗試使用從查詢返回的MongoDB文檔發出請求,因爲服務器上的NodeJS.But中的請求正文出現錯誤:無效的JSON。下面是我嘗試後如何將MongoDB文檔轉換爲JSON對象

{ 
    "_id" : ObjectId("5739a6bf3f1b41477570dc89"), 
    "taskCount" : 2, 
    "study" : "cod", 
    "phase" : "mansa2", 
    "rhimeTaskId" : "5739a6bec4567f6e737fd3db", 
    "recordId" : "5726f3cfc4567f6e737fc3ab", 
    "recordStudy" : "codstudy", 
    "recordPhase" : "mansa2", 
    "recordLanguage" : "Punjabi", 
    "recordScript" : "Latin", 
    "_state" : "CodingComplete", 
    "tasks" : [ 
     { 
      "physician" : ObjectId("5739a6bd3f1b41477570dc78"), 
      "stage" : "Coding", 
      "result" : { 
       "cod" : "C15", 
       "feedback" : { 
        "narrativeLength" : "Adequate", 
        "positiveSymptomsIncluded" : "Only Positive", 
        "certainty" : "High" 
       }, 
       "keywords" : [ 
        "52 yr male, died of food pipe cancer, suffered pain upper abdomen, investigated,FNAC confirmed Cancer, Put on Chemotherapy, multiple cycles, died at home, had fever with chills occasionally" 
       ] 
      } 
     }, 
     { 
      "physician" : ObjectId("5739a6bd3f1b41477570dc79"), 
      "stage" : "Coding", 
      "result" : { 
       "cod" : "C15", 
       "feedback" : { 
        "narrativeLength" : "Inadequate", 
        "positiveSymptomsIncluded" : "Only Positive", 
        "certainty" : "High" 
       }, 
       "keywords" : [ 
        "severe pain abdomen, ultrasonography revealed food pipe cancer, chemotherapy given, died" 
       ] 
      } 
     } 
    ], 
    "__v" : 2 
} 

這裏的文件是我寫使POST請求

var MongoClient = require('mongodb').MongoClient; 
var request = require('request'); 
var assert = require('assert'); 
var cmeprovisioning= 'mongodb://localhost:27017/cmeprovisioning'; 

MongoClient.connect(cmeprovisioning, function(err, db) { 
    assert.equal(null, err); 
    var count=0; 
    console.log("Connected to cmeprovisioning"); 

     var cursor =db.collection('rhimeReport').find(
        {"study":"cod","phase":"mansa2","recordStudy":"codstudy", 
        "recordPhase":"mansa2","_state":"CodingComplete" 
        }); 


       cursor.each(function(err, doc) { 
         assert.equal(err, null); 
         if (doc != null) { 
         console.dir(doc); 
         count=count+1; 
         request({url: "http://cme.host.net:8081/cme-provisioning/update", 
            method: "POST",json: true, 
            headers: {"content-type": "application/json"}, 
            json: doc 
           },function(e,r,b){ 

             console.log("POST Error "+count+" "+e) 
             console.log("POST Response "+count+" "+r) 
             console.log("POST BODY "+count+" "+b) 
           }); 


         } else { 
         console.log("Some Error : "+err) 
         } 
        }); 
}); 

我透過JSON.stringify(DOC)也嘗試了代碼,但仍然收到無效的JSON錯誤。有沒有辦法可以使用查找查詢返回的mongo文檔並將其轉換爲JSON以發出POST請求。

我認爲這些ObjectID是使它成爲一個無效的JSON文檔。

+0

https://docs.mongodb.com/manual/reference/method/ObjectId.toString/ – MegaMind

回答

0

你的問題是你的對象ID, 嘗試用以下:

{ 
    "_id": "5739a6bf3f1b41477570dc89", 
    "taskCount": 2, 
    "study": "cod", 
    "phase": "mansa2", 
    "rhimeTaskId": "5739a6bec4567f6e737fd3db", 
    "recordId": "5726f3cfc4567f6e737fc3ab", 
    "recordStudy": "codstudy", 
    "recordPhase": "mansa2", 
    "recordLanguage": "Punjabi", 
    "recordScript": "Latin", 
    "_state": "CodingComplete", 
    "tasks": [{ 
     "physician": "5739a6bd3f1b41477570dc78", 
     "stage": "Coding", 
     "result": { 
      "cod": "C15", 
      "feedback": { 
       "narrativeLength": "Adequate", 
       "positiveSymptomsIncluded": "Only Positive", 
       "certainty": "High" 
      }, 
      "keywords": [ 
       "52 yr male, died of food pipe cancer, suffered pain upper abdomen, investigated,FNAC confirmed Cancer, Put on Chemotherapy, multiple cycles, died at home, had fever with chills occasionally" 
      ] 
     } 
    }, { 
     "physician": "5739a6bd3f1b41477570dc79", 
     "stage": "Coding", 
     "result": { 
      "cod": "C15", 
      "feedback": { 
       "narrativeLength": "Inadequate", 
       "positiveSymptomsIncluded": "Only Positive", 
       "certainty": "High" 
      }, 
      "keywords": [ 
       "severe pain abdomen, ultrasonography revealed food pipe cancer, chemotherapy given, died" 
      ] 
     } 
    }], 
    "__v": 2 
} 
+0

我怎樣才能使POST請求,因爲每個轉換前的對象ID在節目文檔通過find查詢返回。 –

+0

在發送POST請求之前,根據需要烹飪數據,然後發送到POST請求。 – Subburaj

1

試試這個,

var cursor =db.collection('rhimeReport').find(
     {"study":"cod","phase":"mansa2","recordStudy":"codstudy", 
     "recordPhase":"mansa2","_state":"CodingComplete"}); 

cursor.toString(); 
...... 

希望這有助於。

1

您需要將對象ID轉換爲字符串ie。

var result = { 
 
    "_id": ObjectId("5739a6bf3f1b41477570dc89"), 
 
    "taskCount": 2, 
 
    "study": "cod" 
 
}; 
 
//now conver to string 
 
result=result._id.toString(); 
 
//now you can use the result