来自数据库的 ID 在 API 中不可用
ID From Database Not Usable In API
我使用 Loopback 创建了一个 API,并将其连接到 MongoDB。我通过 JSON 文件将一些数据加载到我的数据库中,并且我可以毫无问题地访问该数据。
全部数据(从 GET /fish 访问)
[
{
"name": "Walleye",
"scientific": "Sander Vitreus",
"environment": "Fresh Water",
"minClimate": "0",
"maxClimate": "0",
"minDepth": "0",
"maxDepth": "27",
"avLength": "54",
"maxLength": "107",
"avWeight": "0",
"maxWeight": "11.3",
"maxAge": "29",
"description": "Occurs in lakes, pools, backwaters, and runs of medium to large rivers. Frequently found in clear water, usually near brush. Prefers large, shallow lakes with high turbidity. Rarely found in brackish waters. Feeds at night, mainly on insects and fishes (prefers yellow perch and freshwater drum but will take any fish available) but feeds on crayfish, snails, frogs, mudpuppies, and small mammals when fish and insects are scarce. Although not widely farmed commercially for consumption, large numbers are hatched and raised for stocking lakes for game fishing. Utilized fresh or frozen; eaten pan-fried, broiled, microwaved and baked.",
"id": "1"
},
{
"name": "Northern Pike",
"scientific": "Esox Lucius",
"environment": "Fresh Water",
"minClimate": "10",
"maxClimate": "28",
"minDepth": "1",
"maxDepth": "5",
"avLength": "55",
"maxLength": "137",
"avWeight": "0",
"maxWeight": "28.4",
"maxAge": "30",
"description": "Occurs in clear vegetated lakes, quiet pools and backwaters of creeks and small to large rivers. Usually solitary and highly territorial. Enters brackish water in the Baltic. Adults feed mainly on fishes, but at times feed heavily on frogs and crayfish. Cannibalism is common. In arctic lakes, it is sometimes the only species present in a given water body. In such cases, juveniles feed on invertebrates and terrestrial vertebrates; large individuals are mainly cannibals. Cannibalistic as juveniles. Feces of pike are avoided by other fish because they contain alarm pheromones. Deposits feces at specific locations, distant from its foraging area. Eggs and young are preyed upon by fishes, aquatic insect larvae, birds, and aquatic mammals. Does not generally undertake long migrations, but a few may move considerable distances. This fish can be heavily infested with parasites, including the broad tapeworm which, if not killed by thorough cooking, can infect human; is used as an intermediate host by a cestode parasite which results to large losses in usable catches of lake whitefish (Coregonus clupeaformis) in some areas; also suffers from a trematode which causes unsightly cysts on the skin. Excellent food fish; utilized fresh and frozen; eaten pan-fried, broiled, and baked. Valuable game fish. In spite of numerous attempts to culture this species, it was never entirely domesticated and does not accept artificial food. Locally impacted by habitat alterations",
"id": "2"
}
]
但是,如果我尝试通过 /fish/{id}
命令访问单个集合,则会出现错误!
错误:
{
"error": {
"statusCode": 404,
"name": "Error",
"message": "Unknown \"fish\" id \"\"1\"\".",
"status": 404,
"code": "MODEL_NOT_FOUND",
"stack": "Error: Unknown \"fish\" id \"\"1\"\".\n at ... (removed long path)...
}
}
如何正确访问单个集合?使用环回,我是否需要将每条鱼添加为环回模型?对我来说,这似乎就是这个错误所说的?
所以我今晚想通了。
首先,我必须编辑 API
中的 JSON
文件
"idInjection": false, //change from true to false
"properties": {
"_id": { //add an id field to the properties
"type": "string",
"id": true,
"generated": true
},
然后我不得不更改加载到数据库中的 JSON 数据。我不得不使用字符串而不是使用整数(1、2、3 等)。所以对于我的 Walleye
模型,我更改了
"_id": "1"
到
"_id": "walleye"
现在我可以毫无问题地使用 GET /fish/{id} 命令了!希望这对遇到它的其他人有所帮助。
我使用 Loopback 创建了一个 API,并将其连接到 MongoDB。我通过 JSON 文件将一些数据加载到我的数据库中,并且我可以毫无问题地访问该数据。
全部数据(从 GET /fish 访问)
[
{
"name": "Walleye",
"scientific": "Sander Vitreus",
"environment": "Fresh Water",
"minClimate": "0",
"maxClimate": "0",
"minDepth": "0",
"maxDepth": "27",
"avLength": "54",
"maxLength": "107",
"avWeight": "0",
"maxWeight": "11.3",
"maxAge": "29",
"description": "Occurs in lakes, pools, backwaters, and runs of medium to large rivers. Frequently found in clear water, usually near brush. Prefers large, shallow lakes with high turbidity. Rarely found in brackish waters. Feeds at night, mainly on insects and fishes (prefers yellow perch and freshwater drum but will take any fish available) but feeds on crayfish, snails, frogs, mudpuppies, and small mammals when fish and insects are scarce. Although not widely farmed commercially for consumption, large numbers are hatched and raised for stocking lakes for game fishing. Utilized fresh or frozen; eaten pan-fried, broiled, microwaved and baked.",
"id": "1"
},
{
"name": "Northern Pike",
"scientific": "Esox Lucius",
"environment": "Fresh Water",
"minClimate": "10",
"maxClimate": "28",
"minDepth": "1",
"maxDepth": "5",
"avLength": "55",
"maxLength": "137",
"avWeight": "0",
"maxWeight": "28.4",
"maxAge": "30",
"description": "Occurs in clear vegetated lakes, quiet pools and backwaters of creeks and small to large rivers. Usually solitary and highly territorial. Enters brackish water in the Baltic. Adults feed mainly on fishes, but at times feed heavily on frogs and crayfish. Cannibalism is common. In arctic lakes, it is sometimes the only species present in a given water body. In such cases, juveniles feed on invertebrates and terrestrial vertebrates; large individuals are mainly cannibals. Cannibalistic as juveniles. Feces of pike are avoided by other fish because they contain alarm pheromones. Deposits feces at specific locations, distant from its foraging area. Eggs and young are preyed upon by fishes, aquatic insect larvae, birds, and aquatic mammals. Does not generally undertake long migrations, but a few may move considerable distances. This fish can be heavily infested with parasites, including the broad tapeworm which, if not killed by thorough cooking, can infect human; is used as an intermediate host by a cestode parasite which results to large losses in usable catches of lake whitefish (Coregonus clupeaformis) in some areas; also suffers from a trematode which causes unsightly cysts on the skin. Excellent food fish; utilized fresh and frozen; eaten pan-fried, broiled, and baked. Valuable game fish. In spite of numerous attempts to culture this species, it was never entirely domesticated and does not accept artificial food. Locally impacted by habitat alterations",
"id": "2"
}
]
但是,如果我尝试通过 /fish/{id}
命令访问单个集合,则会出现错误!
错误:
{
"error": {
"statusCode": 404,
"name": "Error",
"message": "Unknown \"fish\" id \"\"1\"\".",
"status": 404,
"code": "MODEL_NOT_FOUND",
"stack": "Error: Unknown \"fish\" id \"\"1\"\".\n at ... (removed long path)...
}
}
如何正确访问单个集合?使用环回,我是否需要将每条鱼添加为环回模型?对我来说,这似乎就是这个错误所说的?
所以我今晚想通了。
首先,我必须编辑 API
中的JSON
文件
"idInjection": false, //change from true to false
"properties": {
"_id": { //add an id field to the properties
"type": "string",
"id": true,
"generated": true
},
然后我不得不更改加载到数据库中的 JSON 数据。我不得不使用字符串而不是使用整数(1、2、3 等)。所以对于我的 Walleye
模型,我更改了
"_id": "1"
到
"_id": "walleye"
现在我可以毫无问题地使用 GET /fish/{id} 命令了!希望这对遇到它的其他人有所帮助。