loopback-connector-redis
loopback-connector-redis copied to clipboard
Bad value stored for a foreign key that is a string.
Bug or feature request
- [x] Bug
- [ ] Feature request
Description of feature (or steps to reproduce if bug)
In the linked repository there are two models: Profile and Activity. Profiles are stored in ElasticSearch, activities are stored in Redis. A profile has many activities, therefore Loopback automatically injects the property profile_id into the class Activity.
To reproduce the bug follow the steps:
- Start the server and open the API explorer
- Create a new profile, you get a string identifier
- Use the profile ID to create a new Activity, the response should be:
{
"id": "UUID",
"name": "name",
"profile_id": "AV3GqNDiTUO_Ow-hB5vl"
}
- Getting all the activities you have:
[
{
"id": "UUID",
"name": "name",
"profile_id": "\"AV3GqNDiTUO_Ow-hB5vl\""
}
]
- Getting all the activities through the nested route
GET /profiles/{id}/activitiesthe result is an empty array.
Link to sample repo to reproduce issue (if bug)
https://github.com/TwistedLogic/loopback-connector-redis-issue
Expected result
The route GET /profiles/{id}/activities should return all the activities related to the profile identified by {id}
Actual result (if bug)
An empty array is returned by the API
Additional information (Node.js version, LoopBack version, etc)
The profile_id value in the keystore is "\"AV3GqNDiTUO_Ow-hB5vl\"" and not "AV3GqNDiTUO_Ow-hB5vl".
The problem is in the function BridgeToRedis.prototype.forDb, lines 198-200 in current master:
if (!p[i]) {
data[i] = JSON.stringify(data[i]);
continue;
}
JSON.stringify("AV3GqNDiTUO_Ow-hB5vl") === "\"AV3GqNDiTUO_Ow-hB5vl\""
I suppose that the connector doesn't consider the magic property profile_id that loopback automatically injects into the model.
Adding the foreign key to the Activity model like so:
"profile_id": {
"type": "string",
"index": true,
"required": true
}
makes everything work fine, but I believe this is just a workaround.
Ping @raymondfeng any updates about this issue?