1
我試圖用TypeORM和MongoDB來表達,但我遇到了基本問題。TypeORM FindById不適用於MongoDB
我剛剛爲一個實體創建了一個基本的CRUD操作的控制器。方法save,findAll和Filter查找工作正常,但我不能讓需要mongo id的方法工作。
router.get("/", async(req: Request, res: Response) => {
const investmentRepository = getMongoRepository(Investment);
const investments = await investmentRepository.find();
res.send(investments);
});
router.get("/:id", async(req: Request, res: Response) => {
const investmentRepository = getMongoRepository(Investment);
const investment = await
investmentRepository.findOneById(req.params.id);
if (!investment) {
res.status(404);
res.end();
}
res.send(investment);
});
第二種方法總是返回404 例如,這是一個實體返回上得到所有「投資/」
{
"id": "59dfd8cadcbd9d1720457008",
"name": "Teste LCI",
"startDate": 1466305200,
"numberOfDays": 365,
"type": "LCI_LCA"
}
如果我嘗試發送此特定對象調用的請求
投資/ 59dfd8cadcbd9d1720457008
的反應總是404
同樣的行爲的發生是由於刪除方法,拋出一個異常
找不到實體由給定的ID刪除
我也試圖轉換使用字符串對象ID:
new ObjectID(req.params.id);
但它失敗,錯誤對象ID是不是構造。
謝謝,我可以讓它工作導入mongodb。 '*進口從 「MongoDB的」 MongoDB的;' '新mongodb.ObjectId(req.params.id);' 我不'噸知道爲什麼的ObjectID從typeorm包不工作。 –
因爲它只是爲類型聲明「抽象類」,而不是爲了運行時的目的。 在新版本中,您將能夠通過id查找十六進制字符串。 –