因此,我已经在javasript中编写了一个与Mongoose和Express一起使用的API,现在,当我想创建一个帐户时,会出现错误:
“错误:发送标头后无法设置标头”。
我试图和队友一起理解它,但我们不知道发生了什么... 因此,如果您可以帮助我,那将非常友好
创建帐户
router.post('/account/new', (req, res, next) => {
var duplicate = checkForDuplicates(req.body)
if (!duplicate) {
var encryptedPassword;
var clientIP;
bcrypt.hash(req.body['password'], saltRounds, function (err, hash) {
if (err) {
res.status(400).send(err)
}
encryptedPassword = hash;
clientIP = req.connection.remoteAddress;
Account.findOne({ username: req.body['username'] }, (err, account) => {
if (account != null) {
res.send("The account already exists")
} else {
Account.create({ username: req.body['username'], email: req.body['email'], password: encryptedPassword, ip: clientIP }, (err, createdAccount) => {
if (err) {
res.status(400).send(err)
return
}
var informations = {
username: createdAccount.username,
id: createdAccount.id
}
res.status(201).send(informations)
})
}
})
})
} else {
res.status(400).send("The account already exists")
}
});
帐户架构
const mongoose = require("mongoose")
var Schema = mongoose.Schema;
var AccountSchema = new Schema({
username: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
lastip: {
type: String,
default: "0"
},
ip: {
type: String,
default: "0"
},
lastconnect: {
type: Number,
default: 0
},
exolytes: { type: String, default: "0"},
weapons: { type: String, default: "1" },
tools: { type: String, default: "1" },
ships: { type: String, default: "1" },
faction: { type: String, default: "0" },
skins: {
heads: { type: String, default: "1" },
bodys: { type: String, default: "1" }
},
currentSkin: {
head: { type: String, default: "1" },
body: { type: String, default: "1" }
},
ores: {
ore1: { type: Number, default: 0 },
ore2: { type: Number, default: 0 },
ore3: { type: Number, default: 0 },
ore4: { type: Number, default: 0 }
}
}, {
collection: 'accounts'
});
var Account = mongoose.model("Accounts", AccountSchema, "accounts");
module.exports = Account
答案 0 :(得分:0)
您是否发现错误情况下代码将运行
if (err) {
res.status(400).send(err)
}
然后在Account.findOne
中发送响应。
由于响应已经发送,您不能说
res.status(400)
块中的Account.findOne
答案 1 :(得分:0)
每次使用res.send()
时都会增加回报,在这种情况下会有所帮助。
router.post('/account/new', (req, res, next) => {
var duplicate = checkForDuplicates(req.body)
if (!duplicate) {
var encryptedPassword;
var clientIP;
bcrypt.hash(req.body['password'], saltRounds, function (err, hash) {
if (err) {
return res.status(400).send(err)
}
encryptedPassword = hash;
clientIP = req.connection.remoteAddress;
Account.findOne({ username: req.body['username'] }, (err, account) => {
if (account != null) {
return res.send("The account already exists")
} else {
Account.create({ username: req.body['username'], email: req.body['email'], password: encryptedPassword, ip: clientIP }, (err, createdAccount) => {
if (err) {
return res.status(400).send(err)
}
var informations = {
username: createdAccount.username,
id: createdAccount.id
}
return res.status(201).send(informations)
})
}
})
})
} else {
return res.status(400).send("The account already exists")
}
});