I have two models usermodal,my_another_model. usermodel has a hasOne relationship with my_another_model. Whenever I access the given endpoint I get the following error. This happens only when the my_another_model does not have any data. The issue happens when I GET the endpoint /usermodel/me/myAnotherModel
/usermodel/me/myAnotherModel
{
"name": "usermodel",
"base": "User",
"idInjection": true,
"options": {
"validateUpsert": true
},
"mixins": {
},
"properties": {
"first_name": {
"type": "string",
"required": true
},
"last_name": {
"type": "string",
"required": true
},
},
"validations": [],
"relations": {
"myAnotherModel": {
"type": "hasOne",
"model": "my_another_model",
"foreignKey": "",
"options": {
"nestRemoting": true
}
}
},
"acls": [
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "ALLOW"
},
],
"methods": {
}
}
{
"name": "my_another_model",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"required": true
},
"phone": {
"type": "string"
},
"email": {
"type": "string"
}
},
"validations": [],
"relations": {
"usermodel": {
"type": "belongsTo",
"model": "usermodel",
"foreignKey": ""
}
},
"acls": [],
"methods": {}
}
I get the following error message,
Unknown "my_another_model" id "undefined". (err.code=MODEL_NOT_FOUND)
Error: Unknown "my_another_model" id "undefined".
Any help appreciated.
@sujeshthekkepatt thank you for reporting the issue. Please create a small app we can run to reproduce the issue, see https://loopback.io/doc/en/contrib/Reporting-issues.html#loopback-3x-bugs
@bajtos I have added the necessary code for reproduction. The forked repo can be found here.
/usermodels/usermodels/{id}/myanothermodels. Here me can be used instead of id./usermodels/{id}/myanothermodels. Here also me can be used instead of id. Now we can retrieve the instance./usermodels/{id}/myanothermodels. /usermodels/{id}/myanothermodels we get the following error {
"error": {
"statusCode": 404,
"name": "Error",
"message": "Unknown \"myanothermodel\" id \"undefined\".",
"code": "MODEL_NOT_FOUND"
}
}@bajtos Any updates ?
create(post) a
/usermodels/{id}/myanothermodels. Here me can be used instead of id.
Just for the information: it's necessary to login and set the access token in API Explorer first, before calling this endpoint.
Other steps work as described and I did receive the same error as you.
I am also able to reproduce the problem when I call GET /usermodels/{id}/myanothermodels right after creating the user and logging in, it's not necessary to create and delete myanothermodel.
The weird part is that we have a test in loopback to verify that 404 Not Found error is returned when the target model of HasOne relation was not found, see here:
Model definitions:
Oh! I think I see the problem now. The application correctly returns HTTP status code 404, but the error message is misleading.
{
"error": {
"statusCode": 404,
"name": "Error",
"message": "Unknown \"myanothermodel\" id \"undefined\".",
"code": "MODEL_NOT_FOUND"
}
}
@sujeshthekkepatt could you please confirm you are seeing 404 status code too?
What happens under the hood: LoopBack's REST API layer calls juggler's method to retrieve the target model of HasOne relation. This methods returns null because no target was found. LoopBack's REST API layer converts null to 404 Not Found response.
Here is the relevant code:
Maybe we can improve the error message as follows?
var fk = ctx.getArgByName('fk');
var msg = fk
? g.f('Unknown "%s" id "%s".', toModelName, fk)
: g.f('No "%s" instance(s) found', toModelName);
var error = new Error(msg);
With this change in place, I am getting the following HTTP response:
{
"error": {
"statusCode": 404,
"name": "Error",
"message": "No \"myanothermodel\" instance(s) found",
"code": "MODEL_NOT_FOUND"
}
}
Thoughts?
@bajtos yes I am also getting 404. Yep the current error message is not a proper one. But returning null in this scenario is a recommended one,right ? So that we could easily check for null value and continue with our logic. But throwing error makes it bit confusing. And it introduces unnecessary error handling. Error message improvement helps to understand what happened under the hood properly but not helping to write any efficient code. So I think returning an object when there is data and not returning any object when there is no data corresponding to the target model like we do with other operations is best in this scenario. What do you think @bajtos ??
Any update on this @bajtos ?
The current behavior is intentional. The 404 error object is created only when you call the endpoint via REST API. Without this interception, your HTTP clients will get 204 No Content response. IMO, 404 Not Found is more appropriate - it better describes what happened.
@bajtos In that case as you suggested, improving the error message seems a perfect fit. May be I can give a PR .
In that case as you suggested, improving the error message seems a perfect fit. May be I can give a PR .
Great! See our Contributing guide and Submitting a pull request to LoopBack 4 to get started.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.