'Is there a way to avoid stringValue Favicon.ico error in express js

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);


Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sagar