Search code examples
loopbackupdate-allloopback3

Loopback 3 updateAll filter is not working


I have the following model in Loopback 3:

    {
      "name": "rehearsalTest",
      "base": "PersistedModel",
      "idInjection": true,
      "options": {
        "validateUpsert": true
      },
      "properties": {
        "creationDate": {
          "type": "date"
        },
        "userId": {
          "type": "string",
          "required": true
        },
        "courseId": {
          "type": "string",
          "required": true
        },
        "templateId": {
          "type": "string",
          "required": true
        },
        "testId": {
          "type": "string",
          "required": true
        },
        "grade": {
          "type": "string",
          "required": false
        }
      },
      "validations": [],
      "relations": {},
      "acls": [
        {
          "accessType": "*",
          "principalType": "ROLE",
          "principalId": "$unauthenticated",
          "permission": "DENY"
        },
        {
          "accessType": "*",
          "principalType": "ROLE",
          "principalId": "$authenticated",
          "permission": "ALLOW"
        }
      ],
      "methods": {}
    }

It seems pretty trivial, and my data in Mongo is shown like this:

enter image description here

I just want to update the grade in an already created row, filtering by testId. I've done this multiple times, but this one doesn't work, for reasons I cannot seem to fathom.

First I tried with:

await RehearsalTest.updateAll({testId: testId}, {grade: grade}, (err, data) => { 
  console.log('rehearsalTest.updateAll', err, data);
});

This returns a {count: 0}.

So I tried with like, because sometimes it works:

await RehearsalTest.updateAll({testId: {like: testId}}, {grade: grade}, (err, data) => { 
  console.log('rehearsalTest.updateAll', err, data);
});

Which throws this exception:

Error: The testId property has invalid clause {"like":"60afbc93f30a9f7807fc95d1"}: Expected a string or RegExp

The funny thing being that if I go to the loopback explorer and filter, it works flawlessly:

enter image description here

I tried also with the find and findOne, without luck. Either it doesn't return anything without the like clause, or it returns the exception shown above.

Any ideas? I'll be forever thankful.

EDIT It seems that the string that is in the database is not exactly the same as the one I'm comparing it to. But I haven't found out exactly what is happening. This console.log:

console.log(`"${rehearsalTest.testId}"`, `"${testId}"`,
          `${rehearsalTest.testId}`, `${testId}`,        
          `"${rehearsalTest.testId}"` === `"${testId}"`,
          `${rehearsalTest.testId}` === `${testId}`,
          rehearsalTest.testId === testId,
          rehearsalTest.testId == testId,
          );          

returns:

"60b652d4e31a1d54e086570d" "60b652d4e31a1d54e086570d" 60b652d4e31a1d54e086570d 60b652d4e31a1d54e086570d 
true true false true

So obviously something is making these two strings not completely equal, but right now I can't really see what.


Solution

  • So the problem was that my function was receiving testId directly from the object test, as test.id.

    This id, even if it's displayed as a string in the console.log, internally it's an object, so nothing was comparing correctly because of this.

    When I changed test.id with "${test.id}" everything worked.

    I'll proceed to kill myself now, thank you.