我现在很困惑 . 我不明白为什么我收到这个错误 . 这个问题的原因主要是bidder变量 . 当它是一个数字时,没有问题 . 但是一旦我将它设置为字符串,它就会抛出400错误 . 这是我收藏的架构 .

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

var TransactionSchema = new Schema({
  saleNumber: Number,
  bidder: String,
  purchaseAmount: Number,
  purchaseType: String
},{
    collection: 'Transaction'
});

var Transaction = mongoose.model('Transaction', TransactionSchema);
module.exports = Transaction;

在这个函数中,我正在尝试将文档添加到我的集合中 .

async addNewTransaction() {
  let newTransaction = {
    saleNumber: this.saleNumber,
    bidder: this.bidder,
    purchaseAmount: this.purchaseAmount,
    purchaseType: this.purchaseType
  }
  let uri = `http://${process.env.HOST_NAME}:8081/transaction/add`
  await this.axios.post(uri, newTransaction).then((response) => {
    console.log(response)
  })
},

这是我添加新文档的途径:

// Add a transaction
transactionRoutes.route('/add').post(function (req, res) {
  var transaction = new Transaction(req.body)
  if (transaction.saleNumber && transaction.bidder) {
    transaction.save()
    .then(transaction => {
      res.status(200).json({'transaction': 'transaction added successfully'})
    })
    .catch(err => {
      res.status(400).send("unable to save to database")
    })
  }
})

Update 删除了整个集合并开始了一个新集合,这解决了我的问题!