簡體   English   中英

微風Mongo Angular autoGeneratedKey錯誤

[英]Breeze Mongo Angular autoGeneratedKey error

我在Angular和MongoDB中使用Breeze。

我提供了所有必需的服務和腳本,以確保微風可用於angular和MongoDB。

但是,當我嘗試保存更改時,在服務器上出現以下錯誤:

ObjectIds and Guids are the only autoGenerated key types that Breeze currently supports, not: undefined 

此錯誤發生在mongobreeze模塊的mongoSaveHandler.js文件中:

var keyDataType = entityType.keyDataType;
            if (keyDataType === "Guid") {
                e._id = createGuid();
            } else if (keyDataType == "MongoObjectId") {
                // instead of omitting the _id and having mongo update it, we want to set it ourselves so that we can do
                // fk fixup before going async
                e._id = new ObjectID();
            } else {
                that._raiseError(new Error("ObjectIds and Guids are the only autoGenerated key types that Breeze currently supports, not: " + keyDataType));
                return;
            }

我確保我的對象的id是mongo id:

function addVisit() {
    addType({
        name: 'Visit',
        dataProperties: {
            id: { type: DT.MongoObjectId },
            pain: { type: ID },
            paper: {type: ID},
            consistency: {type: ID}
        }
    });
}

但是的確,當我記錄實體類型對象時,它沒有屬性keyDataType嗎?

如果我只是刪除錯誤,我就能使所有工作正常進行。 然后,我插入的對象在MongoDB中如下所示:

{ id: 5350d4e704a02e1f04000000,
pain: 50,
consistency: 50,
date: Fri Apr 18 2014 08:31:51 GMT+0100 (WEST),
_id: 5350d4e7101b04a9560e660a },

意思是他們有2個ID?

當我嘗試查詢數據庫時,得到一個很好的響應:

[
  {
    "id": "535052f504a02e79c6000000",
    "pain": 50,
    "consistency": 50,
    "_id": "535052f6f672174a4dffffd4"
  },
  {
    "id": "5350d1bb04a02e4e56000000",
    "pain": 50,
    "consistency": 50,
    "date": "2014-04-18T07:18:19.616Z",
    "_id": "5350d1bb101b04a9560e6606"
  },
  {
    "id": "5350d2c104a02e595c000000",
    "pain": 50,
    "consistency": 50,
    "date": "2014-04-18T07:22:41.696Z",
    "_id": "5350d2c1101b04a9560e6607"
  },
]

但是,以某種方式,Breeze無法正確導入它,並且我得到了循環依賴。

在此處輸入圖片說明

這可能與雙ID有關嗎?

您從哪里獲得DT.MongoObjectId? 在微風文檔中未將其列為受支持的數據類型,因此它返回undefined作為類型。 如果您正確生成了ID,為什么不使用不可變的字符串呢?

id : { type: DT.String }

嘗試設置一個命名約定,該約定會將微風的"id"字段轉換為蒙哥的"_id" ,反之亦然。 它將消除雙ID
這是客戶端的代碼:

var convention = new breeze.NamingConvention({
    serverPropertyNameToClient: function (serverPropertyName) {
        switch (serverPropertyName) {
            case '_id':
                return 'id';
            default :
                return serverPropertyName;
        }
    },
    clientPropertyNameToServer: function (clientPropertyName) {
        switch (clientPropertyName) {
            case 'id':
                return '_id';
            default:
                return clientPropertyName;
        }
    }
});

convention.setAsDefault();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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