Skip to content

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

@TwistedLogic

Description

@TwistedLogic

Bug or feature request

  • 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions