Я недавно начал изучать Loopback 3 для POC.Я использую соединитель MongoDB, и у меня есть сценарий использования, где есть документ с вложенной записью в MongoDB, с которым можно обойти.Я попробовал все возможные способы, которые я мог понять в соответствии с документацией, чтобы преобразовать автоматический Id в MongoId и вставить во вложенный массив, но, тем не менее, он вставляется только в виде строки.Мой пример использования здесь:
Profile.json
{
"name": "profile",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"required": true
},
"mobile": {
"type": "string",
"required": true
},
"isActive": {
"type": "boolean",
"required": true,
"default": true
},
"isBusy": {
"type": "boolean",
"required": true,
"default": true
}
},
"validations": [],
"relations": {
"customer": {
"type": "belongsTo",
"model": "customer",
"foreignKey": "userId",
"options": {
"nestRemoting": true
}
},
"address": {
"type": "embedsOne",
"model": "address",
"foreignKey": "",
"property": "address",
"options": {
"nestRemoting": true
}
},
"plants": {
"type": "embedsMany",
"model": "plants",
"property": "plants",
"foreignKey": "",
"options": {
"nestRemoting": true,
"forceId": true,
"persist": true
}
}
},
"acls": [],
"methods": {}
}
Переходная модель: Plant.json
{
"name": "plants",
"base": "Model",
"options": {
"validateUpsert": true,
"strictObjectIDCoercion": true
},
"properties": {
"data": {
"type": "string"
},
"city": {
"type": "string"
},
"state": {
"type": "string"
},
"pin": {
"type": "number"
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
В файле profile.json я пробовал
"options": {
"nestRemoting": true,
"forceId": true,
"persist": true
}
В файле factory.json я пробовал
"idInjection": true,
"id": {
"type": "string",
"id": true,
"defaultFn": "uuid"
}
generated: true
Кто-нибудь делает подобное решение, пожалуйста, предоставьте ваши предложения.Спасибо
Обход файла JS
module.exports = function(Plants) {
Plants.observe('before save', function (ctx, next) {
let id = ObjectId(ctx.instance.id);
ctx.instance.id = id;
next();
});
};