Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CastError: Cast to ObjectId failed for value "favicon.ico" at path "_id" for model

I am learning how to make a node api with the help of a youtube tutorial but I get this error everytime when I run nodemon and go to the localhost. I haven't use nay favicon.ico in any part of my code. Can someone help me?

This is my full code for the movies model

var mongoose = require('mongoose');

//movie schema
var moviesSchema = mongoose.Schema({
   Title: {
       type: String,
       required: true
   },
    Genre: {
        type: String,
        required: true
    },
    Release: {
        type: String,
        required: true
    },
    Director: {
        type: String,
        required: true
    },
    Stars: {
        type: String,
        required: true
    },
    Summary: {
        type: String,
        required: true
    }
});

//export the schema
var Movies = module.exports = mongoose.model('Movies',moviesSchema);

//get movies
module.exports.getMovies = function(callback, limit){
    Movies.find(callback).limit(limit);
};

//get movies by id
module.exports.getMovieById = function(id, callback){
    Movies.findById(id, callback);
};

//add a movie
module.exports.addMovie = function(movie, callback){
    Movies.create(movie, callback);
};

//update a movie
module.exports.updateMovie = function(id, movie, options, callback){
    var query = {_id:id};
    var update = {
        Title : movie.Title,
        Genre : movie.Genre,
        Release : movie.Release,
        Director : movie.Director,
        Stars : movie.Stars,
        Summary : movie.Summary


    };
    Movies.findOneAndUpdate(query, update, options, callback);
};

//delete a movie
module.exports.deleteMovie = function(id, callback){
    var query = {_id:id};
    Movies.remove(query, callback);
};

This is my index.js

var express = require('express');
var bodyParser = require("body-parser");
var mongoose = require('mongoose');

Movie = require('./models/movies');

var app = express();

app.use(bodyParser.json());

//mongoose connection
mongoose.connect('mongodb://localhost/movielist');
var db = mongoose.connection;

//get all movies json format
app.get('/',function(req,res){
    Movie.getMovies(function(err,movies){
        if(err){
            throw err;
        }
        res.json(movies);
    });
});

//get movie by id
app.get('/:_id',function(req,res){
    Movie.getMovieById(req.params._id, function(err,movie){
        if(err){
            throw err;
        }
        res.json(movie);
    });
});

//post new movies
app.post('/',function(req,res){
    var movies = req.body;
    Movie.addMovie(movies,function(err,movies){
        if(err){
            throw err;
        }
        res.json(movies);
    });
});

//update new movies
app.put('/:_id',function(req,res){
    var id = req.params._id;
    var movies = req.body;
    Movie.updateMovie(id, movies, {}, function(err,movies){
        if(err){
            throw err;
        }
        res.json(movies);
    });
});

//delete an existing movie
app.delete('/:_id',function(req,res){
    var id = req.params._id;
    Movie.deleteMovie(id, function(err,movies){
        if(err){
            throw err;
        }
        res.json(movies);
    });
});

app.listen(8000);
console.log("running on port 8000");

From what I can see in the console log is that the error might be from this code

   //get movies by id
module.exports.getMovieById = function(id, callback){
    Movies.findById(id, callback);
};

and in the index.js from this code

Movie.getMovieById(req.params._id, function(err,movie){

But I dont understand that favicon.ico failure

like image 380
Solo Avatar asked Sep 05 '25 03:09

Solo


2 Answers

You need to add a <link> tag to your favicon icon image in the HTML or EJS file header which is at root ('/') and communicating with your server.

Adding the following generic tag will solve the problem:

<link rel="icon" type="image/svg+xml" href="https://example.com/image.svg">

Or if you have your own favicon (any image file, e.g. image.png shown below) file placed in the public folder, then you can use this:

<link rel="icon" href="image.png">

Explanation of the problem: By default, your browser looks for an icon file each time you request a new page; some browsers cache this file after it's found the first time. The <link rel="icon" href="LOCATION"> points the browser to the location of the icon file which by convention is called Favicon.ico. If the href is an external URL, it will fetch the icon from that URL. If the href is a path (e.g. "Favicon.ico"), then it will look inside your public folder, and if it doesn't exist in the public folder then it will be called as a GET route on /Favicon.ico. This triggers your code to add that unwanted entry to the DB.

like image 55
Biswajit B. Avatar answered Sep 09 '25 18:09

Biswajit B.


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.

like image 40
Paul Baribeau Avatar answered Sep 09 '25 16:09

Paul Baribeau