Javascript fs.appendFile返回[对象]

Javascript fs.appendFile返回[对象],javascript,jquery,node.js,Javascript,Jquery,Node.js,console.log以{name:'sean',comments:'Hey'}的形式正确返回req.query(request.query)。但当我尝试使用fs.appendFile将其写入文件时,它是以[object]的形式写入的。以下是服务器代码: app.get('/', function (req, res) { var rq = req.query //Write req.query to a variable console.log("received: ", rq) /

console.log以{name:'sean',comments:'Hey'}的形式正确返回req.query(request.query)。但当我尝试使用fs.appendFile将其写入文件时,它是以[object]的形式写入的。以下是服务器代码:

  app.get('/', function (req, res) {
var rq = req.query  //Write req.query to a variable
console.log("received: ", rq)   //This returns correctly
fs.appendFile('comments.txt', rq, function (req, err) {  //This is where object Object is written
if (err) throw err;
console.log("written: ", rq)  //This returns correctly
});
  res.header('Content-type', 'text/html');
  return res.end('<h1>Hello, World!</h1>');
});
app.get('/',函数(req,res){
var rq=req.query//将req.query写入变量
log(“received:,rq)//这将正确返回
appendFile('comments.txt',rq,function(req,err){//这是写入对象的地方
如果(错误)抛出错误;
log(“writed:,rq)//这将正确返回
});
res.header('Content-type','text/html');
return res.end('Hello,World!');
});

有什么想法吗?谢谢。

在将Javascript对象正确写入文件之前,需要将其序列化为字符串。一种方法是使用
JSON.stringify()


(对
fs.appendFile()
的回调只接收一个参数,
err
,AFAIK)

fsAppend(这是一个异步方法)的回调签名是
function(err){}
而不是
function(req,err){}
fs.appendFile('comments.txt', JSON.stringify(rq), function(err) {
  ...
});