[英]MongoDB _id as a String (MEAN Stack)
TL;DR - I am trying to use a collected value from a form input as a document _id
but am getting a 404. TL; DR-我正在尝试使用来自表单输入的收集值作为文档_id
但得到404。
I've got a modal that opens and collects form data. 我有一个可以打开并收集表单数据的模式。 My first input in the form is: 我在表格中的第一个输入是:
<input type="text" id="name" name="name" data-ng-model="name" />
When I try to modify the Mongo (Mongoose) model, to use name
as the _id
, the form wont post. 当我尝试修改Mongo(Mongoose)模型时,要使用name
作为_id
,表单不会发布。 I get a 404 from http://sitegoeshere/#!/somethings/whatever_i_type_in_for_name
我从http://sitegoeshere/#!/somethings/whatever_i_type_in_for_name
获得了404
Example model: 示例模型:
var SomethingSchema = new Schema({
_id: {
type: String,
default: 'default',
trim: true
}
}
mongoose.model('Something', SomethingSchema);
And in my Angular controller: 在我的Angular控制器中:
$scope.create = function() {
// Create new Something object
var something = new Somethings ({
_id: this.name
});
// Redirect after save
something.$save(function(response) {
$location.path('somethings/' + response._id);
}, function(errorResponse) {
$scope.error = errorResponse.data.message;
});
};
I've been told that MongoDB allows Strings as the _id type so what gives? 有人告诉我MongoDB允许将Strings用作_id类型,那有什么用呢? Any ideas? 有任何想法吗?
UPDATE: Here's something strange, too. 更新:这也有些奇怪。 I wanted to see if maybe this was a limitation or bug of Mongoose so I got into the database and created two documents: 我想看看这可能是猫鼬的限制或错误,所以我进入数据库并创建了两个文档:
> db.subnets.find().pretty()
{ "_id" : ObjectId("546bef63395b0694d51b5cbe"), "description" : "description!" }
{ "_id" : "mystring", "description" : "more description!" }
When I go to my app and try to pull their individual views up, I can see the data for my custom _id
document but get a 500 Internal Server Error when I try to access the other. 当我转到我的应用程序并尝试调出他们各自的视图时,可以看到我的自定义_id
文档的数据,但是当我尝试访问另一个时会出现500 Internal Server Error。
GET http://localhost:3000/somethings/546bef63395b0694d51b5cbe 500 (Internal Server Error)
GET http://localhost:3000/somethings/mystring 200 OK
问题很可能与this.name
-看起来像是未定义的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.