【发布时间】:2016-03-03 22:10:32
【问题描述】:
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 中它只打印“对象对象”。有什么想法吗??
【问题讨论】:
-
它只会在你的对象上调用
.toString()。您可以使用JSON.stringify()将对象转储为 JSON,或者根据需要编写自己的代码以将对象呈现为字符串。 -
我检查了如何使用 stringify 但它的描述并没有那么详细。能告诉我怎么用吗??
-
@Pointy 我认为您应该将您的评论作为实际答案提出。因为对我来说这是问题的正确答案。
-
不,不是因为我还是迷路了。那么有人可以告诉我我的程序针对这个问题做了什么吗??
标签: javascript node.js writefile