【问题标题】:Include inserted data like ObjectId to response in node/express在节点/快递中包含插入的数据(如 ObjectId)以响应
【发布时间】:2020-03-12 12:12:28
【问题描述】:

我正在尝试在邮递员中插入一个 ObjectId,在 Content-Type 的标头中,我使用 multipart/form-data

并不断收到错误

{
    "errors": {
        "singer": {
            "message": "Cast to ObjectID failed for value \"ObjectId(\"5dcf8dc320742961c8b5a801\")\" at path \"singer\"",
            "name": "CastError",
            "stringValue": "\"ObjectId(\"5dcf8dc320742961c8b5a801\")\"",
            "kind": "ObjectID",
            "value": "ObjectId(\"5dcf8dc320742961c8b5a801\")",
            "path": "singer",
            "reason": {
                "message": "Cast to ObjectId failed for value \"ObjectId(\"5dcf8dc320742961c8b5a801\")\" at path \"singer\"",
                "name": "CastError",
                "stringValue": "\"ObjectId(\"5dcf8dc320742961c8b5a801\")\"",
                "kind": "ObjectId",
                "value": "ObjectId(\"5dcf8dc320742961c8b5a801\")",
                "path": "singer"
            }
        }
    },
    "_message": "Song validation failed",
    "message": "Song validation failed: singer: Cast to ObjectID failed for value \"ObjectId(\"5dcf8dc320742961c8b5a801\")\" at path \"singer\"",
    "name": "ValidationError"
}

这是我的路线代码,并在歌手字段中将 ObjectId 作为 req.body 传递

router.post('/addSoloSong', mp3.single('mp3File'), async (req, res) => {
  console.log(req.file)
  console.log(req.body.singer);

  const newSong = new Song({
    title: req.body.title,
    singer: req.body.singer,
    releaseDate: Date.parse(req.body.releaseDate),
    mp3File: req.file.path,
    createdAt: new Date()
  })
  try {
    let songAdded = await newSong.save()
    res.status(201).json(songAdded)
  } catch (err) {
    console.log(err);

    res.status(400).json(err)
  }
})

并将歌手字段创建为 ObjectId

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

const songSchema = new Schema({
  title: {
    type: String,
    required: true
  },
  singer: {
    type: Schema.Types.ObjectId,
    ref: "Singer",
    required: true
  },
  featureSinger: {
    type: Schema.Types.ObjectId,
    ref: "Singer"
  },
  groupBand: {
    type: Schema.Types.ObjectId,
    ref: 'GroupBand'
  },
  releaseDate: {
    type: Date,
    required: true
  },
  mp3File: {
    type: String,
    required: true,
    ref: "/public/mp3"
  },
  createdAt: {
    type: Date,
    default: null
  },
  updatedAt: {
    type: Date,
    default: null
  },
  deletedAt: {
    type: Date,
    default: null
  },
  assignedAt: {
    type: Date,
    default: null
  }
});

const Song = mongoose.model("Song", songSchema);

module.exports = Song;

我想在将 ObjectId 插入表单数据时传递它,我错过了什么? 我也想问一下邮递员的mp3File,它说,文件不在你的工作目录中?这是什么意思?

【问题讨论】:

    标签: mongodb express postman


    【解决方案1】:

    您将歌手值作为字符串发送。

    var mongoose = require('mongoose');
    
    router.post('/addSoloSong', mp3.single('mp3File'), async (req, res) => {
      console.log(req.file)
      console.log(req.body.singer);
    
      const newSong = new Song({
        title: req.body.title,
        singer: mongoose.Types.ObjectId(req.body.singer),
        releaseDate: Date.parse(req.body.releaseDate),
        mp3File: req.file.path,
        createdAt: new Date()
      })
      try {
        let songAdded = await newSong.save()
        res.status(201).json(songAdded)
      } catch (err) {
        console.log(err);
    
        res.status(400).json(err)
      }
    })
    

    这会起作用。

    【讨论】:

    • 好的,我只需要申请猫鼬。谢谢。我先试试
    • UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:6543) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code 尝试后出现此错误
    • 我知道了!我只是在邮递员中删除了ObjectId这个词,请求歌手字段。
    猜你喜欢
    • 2017-01-19
    • 2018-02-15
    • 2016-04-09
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    • 2022-01-15
    相关资源
    最近更新 更多