回送-使用来自MongoDB的自定义字符串ID的GET模型 [英] Loopback - GET model using custom String ID from MongoDB
问题描述
我正在开发具有环回功能的API,一切正常,直到我决定更改数据库中文档的ID.现在,我不希望它们是自动生成的.
I'm developing an API with loopback, everything worked fine until I decided to change the ids of my documents in the database. Now I don't want them to be auto generated.
现在,我自己设置ID.每当我点击以下端点时,我都会得到未知ID" 404:GET properties/{id}
Now that I'm setting the Id myself. I get an "Unknown id" 404, whenever I hit this endpoint: GET properties/{id}
如何在回送和mongodb中使用自定义ID?
How can I use custom IDs with loopback and mongodb?
每当我达到此终点时:http://localhost:5000/api/properties/20020705171616489678000000
我收到此错误:
Whenever I hit this endpoint: http://localhost:5000/api/properties/20020705171616489678000000
I get this error:
{
"error": {
"name": "Error",
"status": 404,
"message": "Unknown \"Property\" id \"20020705171616489678000000\".",
"statusCode": 404,
"code": "MODEL_NOT_FOUND"
}
}
这是我的model.json,以防万一...
This is my model.json, just in case...
{
"name": "Property",
"plural": "properties",
"base": "PersistedModel",
"idInjection": false,
"options": {
"validateUpsert": true
},
"properties": {
"id": {"id": true, "type": "string", "generated": false},
"photos": {
"type": [
"string"
]
},
"propertyType": {
"type": "string",
"required": true
},
"internalId": {
"type": "string",
"required": true
},
"flexCode": {
"type": "string",
"required": true
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": []
}
推荐答案
当我尝试使用带有文本ID字段的较小数字的PostGreSQL DB安装程序时,您的模型设置(与idInjection: true
或false
一起使用)可以正常工作
Your model setup (with with idInjection: true
or false
) did work when I tried it with a PostGreSQL DB setup with a text id field for smaller numbers.
使用DEBUG=loopback:connector:* node .
运行回送应用程序将输出在终端中运行的数据库查询-我尝试使用您正在尝试的id值来尝试它,并且参数值是[2.002070517161649e+25]
,所以数字的大小是个问题.
Running a Loopback application with DEBUG=loopback:connector:* node .
outputs the database queries being run in the terminal - I tried it with the id value you are trying and the parameter value was [2.002070517161649e+25]
, so the size of the number is the issue.
您可以尝试将其作为Loopback中的错误进行举报,但是JS处理大量数字很可怕,因此最好还是不要使用诸如标识符之类的大量数字.
You could try raising it as a bug in Loopback, but JS is horrible at dealing with large numbers so you may be better off not using such large numbers as identifiers anyway.
如果ID是一个超过16个字符的字母数字字符串,它确实可以工作,因此可能要为您解决(使用ObjectId?),具体取决于您要实现的目标.
It does work if the ID is an alphanumeric string over 16 characters so there might be a work around for you (use ObjectId?), depending on what you are trying to achieve.
这篇关于回送-使用来自MongoDB的自定义字符串ID的GET模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!