[英]Error: Can't set headers after they are sent. comes from calling the smart contract function
我写了一个智能合约来创建简单的数据库。 它包含多种功能,当我第一次点击REST api时,它会为我提供正确的输出,但是如果再点击一次,它会使节点服务器崩溃并显示一条消息-
F:\\ project \\ blockchain \\ node_modules \\ solc \\ soljson.js:1(函数(导出,需求,模块,__ filename,__ dirname){var Module; if(!Module)Module =(typeof Module!==“ undefined”? Module:null)|| {}; var moduleOverrides = {}; for(模块中的var键){if(Module .hasOwnProperty(key)){moduleOverrides [key] = Module [key]}} var ENVIRONMENT_IS_WEB = typeof window = ==“ object”; var ENVIRONMENT_IS_WORKER =类型为importScripts ===“功能”; var ENVIRONMENT_IS_NODE =进程类型===“ object” && typeof require ===“ function” &&!ENVIRONMENT_IS_WEB &&! ENVIRONMENT_IS_NODE &&!ENVIRONMENT_IS_WORKER; if(EN VIRONMENT_IS_NODE){if(!Module [“ print”])Module [“ print”] = function print(x){process [“ stdout”]。write(x +“ \\ n”)} ;; if(!Module [“ printErr”])Module [“ printErr”] =函数printErr(x){process [“ stderr”]。write(x +“ \\ n”)}; var nodeFS = require(“ fs”); var nodePath = require(“ path”); Module [“ read”] = function read(filename,binary){filename = nodePath“ normalize”; var ret = nodeFS [“ readFileSync”](文件
错误:发送标头后无法设置标头。 在ServerResponse.send(F:\\ project)的ServerResponse.header(F:\\ project \\ blockchain \\ node_modules \\ express \\ lib \\ response.js:725:10)的ServerResponse.OutgoingMessage.setHeader(_http_outgoing.js:344:11)处位于ServerResponse.json的\\ blockchain \\ node_modules \\ express \\ lib \\ response.js:170:12(F:\\ project \\ blockchain \\ node_modules \\ express \\ lib \\ response.js:256:15)位于F:\\ project \\ blockchain \\ Flock:\\ project \\ blockchain \\ node_modules \\ web3 \\ lib \\ web3 \\ filter.js:120:21的\\ BlockchainDB \\ users \\ userFunction.js:37:47在F:\\ project \\ blockchain \\的Array.forEach(native) Array.forEach处的node_modules \\ web3 \\ lib \\ web3 \\ filter.js:119:32(本机)at Object.onMessage [作为回调](F:\\ project \\ blockchain \\ node_modules \\ web3 \\ lib \\ web3 \\ filter.js: 117:22)
我的api代码
module.exports.creation=function(req,res){
var user_id = req.params.user_id || req.body.user_id;
var currency = req.params.user_id || req.body.currency ;
if(user_id == undefined || currency == undefined){
res.status(500).json({status:false,errors:"Empty parameters"});
}else{
isaccount().then(function(accountNumber){
if(accountNumber!=null){
var timestamp = Date.now();
var result = contract.createUser.sendTransaction(user_id,currency,accountNumber,timestamp,{from:web3.eth.coinbase,gas:600000});
var event = contract.CreatedUser(function(error, result) {
if (!error){
if(result.args.userId!= undefined && result.args.accountId!= undefined && result.args.categoryId!= undefined){
logger.info("user created with id :"+result);
res.status(200).json({status:true,data:{iduser:result.args.userId,accountId:result.args.accountId,created_at:timestamp}});
}else{
logger.info("user creation have problem :");
res.status(500).json({status:false,errors:"no response"});
}
}else{
logger.error(error);
res.status(500).send(false);
}
});
}else{
logger.debug("Problem in accountNumber generation");
res.status(500).send(false);
}
});
}
}
请帮我解决这个问题
提前致谢
尝试对所有res.status()
调用使用return
语句。 因此,即使将响应状态设置为某种状态,它之后的某处也会再次获得res.status()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.