I am developing a simple data persistence app using mongoose, After getting stuck on this error
我正在使用mongoose开发一个简单的数据持久性应用程序,之后遇到了这个错误
CastError: Cast to ObjectId failed for value "{ _id: 'id' }" at path "_id" for model 'foo'
CastError:对于模型'foo',对于路径“_id”的值“{_id:'id'}”,Cast to ObjectId失败
i tried to use mongoose.Types.ObjectId
as suggested by various threads, one partcular : https://*.com/a/17223701/4206519, but now I am getting a new error:
我尝试使用各种线程建议的mongoose.Types.ObjectId,一个部分:https://*.com/a/17223701/4206519,但现在我收到一个新错误:
TypeError: hex is not a function.
TypeError:hex不是函数。
Here is a relevant part of the code:
以下是代码的相关部分:
app.get('/campgrounds/:id', function(req, res){
var id = req.params.id;
var ObjectId = mongoose.Types.ObjectId(id);
Campground.findById(ObjectId, function(err, found){
if (err) {
console.log(err);
} else {
//render show template with that campground
res.render('show.ejs', {campground: found});
}
});
});
app.listen(3000, function(){
console.log("server has started");
});
Being a newbie, i may be making a simple mistake here, any help will be appreciated.
作为一个新手,我可能在这里犯了一个简单的错误,任何帮助将不胜感激。
3 个解决方案
#1
6
From last 2 days i am also getting the same problem and it's due to the version issue
从过去2天起,我也遇到了同样的问题,这是由于版本问题
i was using these version "mongodb": "^2.2.19",
我正在使用这些版本“mongodb”:“^ 2.2.19”,
"mongoose": "^4.7.6", and getting the error that Hex is not a function
“mongoose”:“^ 4.7.6”,并且得到Hex不是函数的错误
then i change the version to "mongodb": "2.1.7", "mongoose": "4.4.8"
然后我将版本更改为“mongodb”:“2.1.7”,“mongoose”:“4.4.8”
and it start working so i think they have removed the hex function and other so try after installing this version in your package.json dont use ^before version name just add "mongodb": "2.1.7", "mongoose": "4.4.8" and install
并且它开始工作所以我认为他们已经删除了十六进制函数和其他所以尝试在你的package.json中安装此版本之后不要使用^版本名称之前添加“mongodb”:“2.1.7”,“mongoose”:“4.4 .8“并安装
#2
1
Remove var ObjectId = mongoose.Types.ObjectId(id);
and it should work ...And pass the id instead of the ObjectId in the findById function :)
删除var ObjectId = mongoose.Types.ObjectId(id);它应该工作...并在findById函数中传递id而不是ObjectId :)
#3
1
If you are using Mongodb driver , you can do like
如果你使用Mongodb驱动程序,你可以这样做
var ObjectID = require('mongodb').ObjectID
var id = new ObjectID(req.params.id); // Hex
Mongoose
var mongoose = require('mongoose');
var id = mongoose.Types.ObjectId('4edd40c86762e0fb12000003');
var _id = mongoose.mongo.ObjectId("4eb6e7e7e9b7f4194e000001");
console.log(id);
console.log(_id);
//4edd40c86762e0fb12000003
//4eb6e7e7e9b7f4194e000001
How to use in findById
如何在findById中使用
Campground.findById(id.toString(), function (err, found) {
// Do Whatever you like after getting data
} );
#1
6
From last 2 days i am also getting the same problem and it's due to the version issue
从过去2天起,我也遇到了同样的问题,这是由于版本问题
i was using these version "mongodb": "^2.2.19",
我正在使用这些版本“mongodb”:“^ 2.2.19”,
"mongoose": "^4.7.6", and getting the error that Hex is not a function
“mongoose”:“^ 4.7.6”,并且得到Hex不是函数的错误
then i change the version to "mongodb": "2.1.7", "mongoose": "4.4.8"
然后我将版本更改为“mongodb”:“2.1.7”,“mongoose”:“4.4.8”
and it start working so i think they have removed the hex function and other so try after installing this version in your package.json dont use ^before version name just add "mongodb": "2.1.7", "mongoose": "4.4.8" and install
并且它开始工作所以我认为他们已经删除了十六进制函数和其他所以尝试在你的package.json中安装此版本之后不要使用^版本名称之前添加“mongodb”:“2.1.7”,“mongoose”:“4.4 .8“并安装
#2
1
Remove var ObjectId = mongoose.Types.ObjectId(id);
and it should work ...And pass the id instead of the ObjectId in the findById function :)
删除var ObjectId = mongoose.Types.ObjectId(id);它应该工作...并在findById函数中传递id而不是ObjectId :)
#3
1
If you are using Mongodb driver , you can do like
如果你使用Mongodb驱动程序,你可以这样做
var ObjectID = require('mongodb').ObjectID
var id = new ObjectID(req.params.id); // Hex
Mongoose
var mongoose = require('mongoose');
var id = mongoose.Types.ObjectId('4edd40c86762e0fb12000003');
var _id = mongoose.mongo.ObjectId("4eb6e7e7e9b7f4194e000001");
console.log(id);
console.log(_id);
//4edd40c86762e0fb12000003
//4eb6e7e7e9b7f4194e000001
How to use in findById
如何在findById中使用
Campground.findById(id.toString(), function (err, found) {
// Do Whatever you like after getting data
} );