UnhandledPromiseRejectionWarning: CastError: Cast to string failed for value in mongoose

Posted

技术标签:

【中文标题】UnhandledPromiseRejectionWarning: CastError: Cast to string failed for value in mongoose【英文标题】: 【发布时间】:2021-01-07 13:45:12 【问题描述】:

我已经实现了一种使用 nodejs 从 mongodb 检索一些数据的方法。一旦我执行该方法,它就会在控制台中出现以下错误

(node:7960) UnhandledPromiseRejectionWarning: CastError: Cast to string failed for value "
  Unavailable: [
     day: 'Monday', startTime: '10:30', endTime: '12.30' ,
     day: 'Tuesday', startTime: '11:00', endTime: '12.00' 
  ],
  _id: 5f2822f522dec4051444bf2c,
  building: 'Computing',
  room: 'A-301',
  capacity: 250,
  type: 'Lecture hall',
  __v: 0
" at path "room" for model "Room"
    at model.Query.exec (/Users/applefactory/IdeaProjects/time_table_generation_chernobyl/backend/node_modules/mongoose/lib/query.js:4351:21)
    at model.Query.Query.then (/Users/applefactory/IdeaProjects/time_table_generation_chernobyl/backend/node_modules/mongoose/lib/query.js:4443:15)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:7960) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:7960) [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.

方法的实现:

async function getRoomDetails(_room)

    let preferredRoomDetails="";
// console.log('Start')
    preferredRoomDetails = await Rooms.findOne(room : _room);
// console.log("End")
    console.log(preferredRoomDetails)
    return preferredRoomDetails;

(通过使用控制台日志,我能够找到错误发生的确切位置,并且发生在上述方法中)

房间模型类:

const mongoose = require('mongoose');

const schema = mongoose.Schema;

const RoomSchema = new schema(
    building : 
        type: String,
        required: true,
        unique:true,
    ,
    room:
        type: String,
        required: true,
        unique:true
    ,
    capacity:
        type: Number,
        required: true,
        unique:true
    ,
    type:
        type: String,
        required: true,
        unique:true
    ,
    Unavailable:
        type:Array
    
);

const Room= mongoose.model('Room',RoomSchema);

module.exports = Room;

我该如何解决这个问题?感谢您的宝贵时间!

【问题讨论】:

【参考方案1】:

也许检查函数“getRoomDetails”的“_room”参数:

async function getRoomDetails(_room)

看起来你正在传递一个对象?如果是这样,你可能想改变

    preferredRoomDetails = await Rooms.findOne(room : _room);

    preferredRoomDetails = await Rooms.findOne(room : _room._id);

【讨论】:

以上是关于UnhandledPromiseRejectionWarning: CastError: Cast to string failed for value in mongoose的主要内容,如果未能解决你的问题,请参考以下文章

[Unhandled promise rejection: TypeError: null is not an object (evaluating '_reactNativeImageCropPic

等待 - 捕获错误 - UnhandledPromiseRejectionWarning

批量删除如何工作?

7月工作知识总计:

未处理的承诺拒绝 |重启命令

未处理的承诺拒绝警告(Cordova Angular)