在ftp库中发送后无法设置标题

在ftp库中发送后无法设置标题

问题描述:

我在npm上使用ftp库。我面对这个错误在ftp库中发送后无法设置标题

_http_outgoing.js:357 
throw new Error('Can\'t set headers after they are sent.'); 
^ 

Error: Can't set headers after they are sent. 
    at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:357:11) 
    at ServerResponse.header (/Users/nomanmaqsood/Documents/netsuite-ftp/node_modules/express/lib/response.js:730:10) 

我第一次得到在第二图书馆应有的反应叫我节点应用程序崩溃是由于上述错误。这里是我的代码亲切指导我哪里是错误

c.on ('ready', function() { 
      c.list (function (err, list) { 
       if (err) { 
        c.end(); 
       } else { 
        if (list.length > 0) { 
         c.end(); 
         return res.status(200).json({data:list});//crash here on 2nd call 
        } 
       } 
      }); 
     }); 

请帮我

+0

看起来像你发送响应之前设置标题。也许单独的res.status(200).json()分成两行,例如。 res.status(200),res.json(...); – Terrance00

检查你不叫res.jsonres.send两次在一个客户端的HTTP请求。

尝试

c.on ('ready', function() { 
    c.list (function (err, list) { 
     if (err) 
      return res.status(500).json({error: err.message}); 

     c.end(); 
     res.status(200).json({data: list || []}); 
    }); 
}); 

附:我想你使用Express

+0

是的,我正在使用express,与此代码同样的错误 – DynaSoft

c是全局对象,它注册了两次。只是使物体本地和东西的作品像魅力