3
function querydb(){
cursor = dbConnection.collection("logs").find(queryObj).limit(valueList[1]);
/*Either decide here to foreach for file or for console OR...*/
cursor.forEach(function(item){
/*Decide here whether to write to file or console*/
if(valueList[0] != null){
fs.writeFile(valueList[0], item, 'utf-8', console.log("finished!!"));
}
if(valueList[0] == null){
console.log(item);
console.log("Done");
}
/*CHECK TO SEE IF THE CURSOR IS EXHAUSTED*/
if(cursor == null){
//now we can close the database
dbConnection.close();
}
}
);
};
這裏是我的querydb函數。正如你所看到的,我檢查這個值是否等於null,如果它是真的,我調用writeFile。但在.txt中它只是打印「對象對象」。任何想法發生什麼事?NodeJs writeFile在txt文件中寫入「object object」而不是實際的對象元素
它只是要在您的對象上調用'.toString()'。您可以使用'JSON.stringify()'將對象轉儲爲JSON,或者編寫自己的代碼以根據需要將對象呈現爲字符串。 – Pointy
我檢查瞭如何使用stringify,但沒有詳細描述它。你能告訴我如何使用它? –
@Pointy我認爲你應該提出你的評論作爲一個實際的答案。因爲對我來說,這是問題的正確答案。 – yeiniel