问题描述
router/oneBuy.js代码:
var controller = require(’../controller/oneBuy’);module.exports = function(app){ //首页 app.post(’/createOneBuyActive’, controller.createOneBuyActive);};
controller/oneBuy.js代码:
var OneBuyModel = require(’../model/OneBuy’);module.exports = { async createOneBuyActive () { let self = this let params = self.request.body; let onebuy = {}; onebuy.id = params.id; onebuy.name = params.name; onebuy.des = params.des; onebuy.createTime = new Date().getTime() + ’’; // 校验参数 try { if (!onebuy.name.length) {self.status = 401self.body = {retCode: 401, retMsg: ’请填写活动名称’} } if (!onebuy.des.length) {self.status = 401self.body = {retCode: 401, retMsg: ’请填写活动描述’} } let data = await OneBuyModel.create(onebuy) console.log(data) if (data.result.ok === 1) {self.status = 200self.body = {retCode: 200, retMsg: ’新增活动成功!’} } else {self.status = 400self.body = {retCode: 400, retMsg: ’新增活动失败!’} } } catch (e) { self.status = 500 self.body = {retCode: 500, retMsg: ’服务器异常’, error: e} // throw new Error(e); } }}
报错:
(node:8516) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error:Can’t remove headers after they are sent(node:8516) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promiserejections that are not handled will terminate the Node.js process with a non-zero exit code.
问题解答
回答1:当给.body赋值后就应该return,否则后面代码还会执行,所以就导致了Can’t remove headers after they are sent