最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

错误,无法设置头它们发送后

运维笔记admin18浏览0评论

错误,无法设置头它们发送后

错误,无法设置头它们发送后

所以,我编写的JavaScript与猫鼬和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
回答如下:

添加每次使用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")
  }
});
发布评论

评论列表(0)

  1. 暂无评论