Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js NodeJs错误:Can';t在发送邮件后设置邮件头_Node.js_Express - Fatal编程技术网

Node.js NodeJs错误:Can';t在发送邮件后设置邮件头

Node.js NodeJs错误:Can';t在发送邮件后设置邮件头,node.js,express,Node.js,Express,我是nodejs新手,遇到了一个错误:发送邮件后无法设置邮件头。下面是我的代码。请帮忙。我用邮递员来测试这段代码。它在前两次点击中起作用,但在第三次点击中,这个错误就来了 const http = require('http'); const fs = require('fs') module.exports.verifyPyeval5 = function(request, response){ let filePath = "D:/PyEval/NewPyEvalRequests

我是nodejs新手,遇到了一个错误:发送邮件后无法设置邮件头。下面是我的代码。请帮忙。我用邮递员来测试这段代码。它在前两次点击中起作用,但在第三次点击中,这个错误就来了

const http = require('http');

const fs = require('fs')

module.exports.verifyPyeval5 = function(request, response){
    let filePath = "D:/PyEval/NewPyEvalRequests/solution.txt";  
    fs.readFile(filePath, 'utf8', function (err,data) {
       var json = JSON.parse(data);
        post_call(json,function(res){
            response.status(200)
        .json(res);
        });
    });   

};

var post_call = function(new_val,callback){
    var post_req = http.request(post_options, function(res) { 
        res.setEncoding('utf8');
        res.on('data', function (chunk) {
            callback(chunk);
        });
      });
      post_req.write(JSON.stringify(new_val));
      post_req.end();

};

var post_options = {
      host: 'acclabserv',
      port: '8080',
      path: '/generate',
      method: 'POST',
      headers: {
          'Content-Type': 'application/json'
      }
  };

我明白了,问题是回调函数会被多次调用

var post_call = function(new_val,callback){
    var post_req = http.request(post_options, function(res) { 
        res.setEncoding('utf8');
        var str = null;
        res.on('data', function (chunk) {
            str += chunk;
        });
      res.on('end', function () {
            callback(str);
        });
      });
      post_req.write(JSON.stringify(new_val));
      post_req.end();

};
还有一件事需要注意,我不知道chuck的类型,如果它是一个对象,并且你需要对象数组作为响应,那么你可以使用这个

var-str=[]//在请求上('data')str.push(chunck)


我只使用了状态和发送链。更新了我的答案是,你在链中使用了它,但没有必要检查答案。我尝试删除状态。但问题依然存在。我找不到callback在哪里被调用了两次。您能帮忙吗?req.on('data')这会被多次调用使用终止回调,如req.on('end')。谢谢您的帮助。我将检查解决方案并进行相应的更新。为什么要使用这一行
post_req.write(JSON.stringify(new_val))它的用途是什么?post_req.write(JSON.stringify(new_val))可能重复;这用于将json写入请求。