有没有办法避免 Express js 中的 stringValue Favicon.ico 错误

发布于 2025-01-10 20:28:13 字数 1052 浏览 0 评论 0原文

我正在尝试在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初见 2025-01-17 20:28:13

我刚刚与这个问题斗争了相当长的一段时间,并尝试了各种各样的修复(包括将我的 Mongoose 版本降级到 4.7.2 或更低版本,这是其他地方建议的),但不幸的是,它们都不适合我。也就是说,虽然我无法解释为什么会发生这种情况*,但有一个简单的修复方法对我有用:

不要向~/:var发出请求。相反,在两者之间添加一个额外的层,以便您的请求转到 ~/string/:var

在你的问题中,你有一个针对“/:”的获取、放置和删除请求。更改这三行代码和任何依赖项,您应该或多或少可以顺利进行。


*我对为什么会发生这种情况的最佳猜测:某些东西无法将“/”路径识别为空,而是同时匹配“/”和“/: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:

Don't make requests to ~/:var. Instead add an additional layer in between, so that your request goes to ~/string/:var.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文