簡體   English   中英

MongoDB _id作為字符串(MEAN堆棧)

[英]MongoDB _id as a String (MEAN Stack)

TL; DR-我正在嘗試使用來自表單輸入的收集值作為文檔_id但得到404。

我有一個可以打開並收集表單數據的模式。 我在表格中的第一個輸入是:

<input type="text" id="name" name="name" data-ng-model="name" />

當我嘗試修改Mongo(Mongoose)模型時,要使用name作為_id ,表單不會發布。 我從http://sitegoeshere/#!/somethings/whatever_i_type_in_for_name獲得了404

示例模型:

var SomethingSchema = new Schema({
   _id: {
       type: String,
       default: 'default',
       trim: true
   }
}

mongoose.model('Something', SomethingSchema);

在我的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;
    });
};

有人告訴我MongoDB允許將Strings用作_id類型,那有什么用呢? 有任何想法嗎?

更新:這也有些奇怪。 我想看看這可能是貓鼬的限制或錯誤,所以我進入數據庫並創建了兩個文檔:

> db.subnets.find().pretty()
{ "_id" : ObjectId("546bef63395b0694d51b5cbe"), "description" : "description!" }
{ "_id" : "mystring", "description" : "more description!" }

當我轉到我的應用程序並嘗試調出他們各自的視圖時,可以看到我的自定義_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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM