问题描述
我正在 youtube 教程的帮助下学习如何制作节点 API,但每次运行 nodemon 并转到 localhost 时,我都会收到此错误.我没有在我的代码的任何部分使用 nay favicon.ico.有人可以帮我吗?
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);
};
这是我的 index.js
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);
};
并在此代码的 index.js 中
and in the index.js from this code
Movie.getMovieById(req.params._id, function(err,movie){
但我不明白 favicon.ico 失败
But I dont understand that favicon.ico failure
推荐答案
//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);
});
get 请求不能有 params .
A get request cant have params .
您应该将其改为发布请求
You should change it to a post request instead
这篇关于CastError:为值“favicon.ico"强制转换为 ObjectId 失败;在路径“_id"处对于模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!