UnhandledPromiseRejectionwarning:CastError: Cast to ObjectId failed for value

简介: UnhandledPromiseRejectionwarning:CastError: Cast to ObjectId failed for value

UnhandledPromiseRejectionwarning:CastError: {解决办法之一}

错误缘由:我是要查询数据库中对应的id,进行数据删除操作。但是得到的结果却是报错。

报错信息为:UnhandledPromiseRejectionwarning:CastError: Cast to ObjectId failed for value " 5ffbb96755245c37047691fa" at path “_id” for model "User


我的错误是req.query.id输出的结果和我手动输入的id值一样,但使用findOne()方法就是查询不出来数据库中对应的数据。然后对照终端显示仔细看了一下,发现可能是因为空格的原因。


因为req.query.id的打印结果有空格导致的错误。


如下所示终端显示空格:

使用了如下代码进行检测得出结论:

if (req.query.id.trim() === '5ffbb8e355245c37047691f4') {
    console.log('结果一样 ')
} else {
    console.log(req.query.id)
}

trim()方法,去掉字符串两边的空格。本次输出结果为:”结果一样“。


目录
相关文章
|
5月前
|
SQL Java 数据库连接
Cause: java.sql.SQLException: Field ‘id‘ doesn‘t have a default value; Field ‘id‘ doesn‘t have a de
Cause: java.sql.SQLException: Field ‘id‘ doesn‘t have a default value; Field ‘id‘ doesn‘t have a de
### Cause: java.sql.SQLException: Field ‘id‘ doesn‘t have a default value; Field ‘id‘ doesn‘t have
### Cause: java.sql.SQLException: Field ‘id‘ doesn‘t have a default value; Field ‘id‘ doesn‘t have
|
数据采集 缓存 Android开发
Assertion desc failed at libswscale/swscale_internal.h:674和ffmpeg的各种key+value
Assertion desc failed at libswscale/swscale_internal.h:674和ffmpeg的各种key+value
239 0
错误代码:88000, 错误信息:without comment privilege hint: [7oJ0533w689] rid: 630432cd-15944cf6-083e04fc
错误代码:88000, 错误信息:without comment privilege hint: [7oJ0533w689] rid: 630432cd-15944cf6-083e04fc
252 0
错误代码:88000, 错误信息:without comment privilege hint: [7oJ0533w689] rid: 630432cd-15944cf6-083e04fc
DB:: Exception: Unknown data type family: DateTime64
DB:: Exception: Unknown data type family: DateTime64
1137 1
|
SQL 关系型数据库 MySQL
MySQL数据库报错 > 1366 - Incorrect string value: ‘\xE6\xB1\x9F\xE6\x96\x87‘ for column ‘Teacher‘ at row 1
MySQL数据库报错 > 1366 - Incorrect string value: ‘\xE6\xB1\x9F\xE6\x96\x87‘ for column ‘Teacher‘ at row 1
284 0
MySQL数据库报错 > 1366 - Incorrect string value: ‘\xE6\xB1\x9F\xE6\x96\x87‘ for column ‘Teacher‘ at row 1
|
SQL 关系型数据库 MySQL
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
1259 0
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1