loopback-connector-redis icon indicating copy to clipboard operation
loopback-connector-redis copied to clipboard

Bad value stored for a foreign key that is a string.

Open TwistedLogic opened this issue 8 years ago • 1 comments

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:

  1. Start the server and open the API explorer
  2. Create a new profile, you get a string identifier
  3. Use the profile ID to create a new Activity, the response should be:
{
  "id": "UUID",
  "name": "name",
  "profile_id": "AV3GqNDiTUO_Ow-hB5vl"
}
  1. Getting all the activities you have:
[
  {
    "id": "UUID",
    "name": "name",
    "profile_id": "\"AV3GqNDiTUO_Ow-hB5vl\""
  }
]
  1. Getting all the activities through the nested route GET /profiles/{id}/activities the 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.

TwistedLogic avatar Aug 09 '17 11:08 TwistedLogic

Ping @raymondfeng any updates about this issue?

yorkie avatar Jul 19 '18 10:07 yorkie