有没有办法避免 Express js 中的 stringValue Favicon.ico 错误
我正在尝试在我的 Express 中构建一个应用程序,每当我导航到管理页面时,我都会收到此错误
const castError = new CastError();
^
CastError: Cast to ObjectId failed for value "favicon.ico" (type string) at path "_id" for model "Post"
,我也会收到此特定错误
messageFormat: undefined,
stringValue: '"favicon.ico"',
kind: 'ObjectId',
value: 'favicon.ico',
path: '_id',
reason: BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters
,但在我的帖子模型中,我有以下代码
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const postSchema = new Schema({
title: {
type: String,
required: true
},
body: {
type: String,
required: true
},
excerpt: {
type: String,
required: true
},
image: {
type: String,
required: true
},
createdAt: {
type: Date,
default: Date.now
},
updatedAt: {
type: Date,
default: Date.now
}
})
module.exports = mongoose.model('Post', postSchema);
I am trying to build an application in my Express and anytime i navigate to the admin page i get this error
const castError = new CastError();
^
CastError: Cast to ObjectId failed for value "favicon.ico" (type string) at path "_id" for model "Post"
i also get this particular error too
messageFormat: undefined,
stringValue: '"favicon.ico"',
kind: 'ObjectId',
value: 'favicon.ico',
path: '_id',
reason: BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters
But in my Post Model i a have the following code below
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const postSchema = new Schema({
title: {
type: String,
required: true
},
body: {
type: String,
required: true
},
excerpt: {
type: String,
required: true
},
image: {
type: String,
required: true
},
createdAt: {
type: Date,
default: Date.now
},
updatedAt: {
type: Date,
default: Date.now
}
})
module.exports = mongoose.model('Post', postSchema);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我刚刚与这个问题斗争了相当长的一段时间,并尝试了各种各样的修复(包括将我的 Mongoose 版本降级到 4.7.2 或更低版本,这是其他地方建议的),但不幸的是,它们都不适合我。也就是说,虽然我无法解释为什么会发生这种情况*,但有一个简单的修复方法对我有用:
在你的问题中,你有一个针对“/:”的获取、放置和删除请求。更改这三行代码和任何依赖项,您应该或多或少可以顺利进行。
*我对为什么会发生这种情况的最佳猜测:某些东西无法将“/”路径识别为空,而是同时匹配“/”和“/:X”。
I just fought with this issue for quite some time and tried a wide variety of fixes (including downgrading my version of Mongoose to 4.7.2 or lower, which was suggested elsewhere), but unfortunately, none of them worked for me. That said, while I can't explain why this is happening*, there was one easy fix that worked for me:
In your question, you have a get, a put, and a delete request for "/:". Alter those three lines of code and any dependencies, and you should be more or less good to go.
*My best guess for why this is happening: something is failing to recognize the "/" path as empty, and instead is matching to both "/" and "/:X" simultaneously.