3

我在 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": {}
    }

看起来很简单,我在 Mongo 中的数据如下所示:

在此处输入图像描述

我只想更新已经创建的行中的成绩,按testId. 我已经多次这样做了,但这一次不起作用,原因我似乎无法理解。

首先我尝试了:

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

这会返回一个{count: 0}.

所以我尝试了like,因为有时它有效:

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

抛出此异常:

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

有趣的是,如果我去环回浏览器和过滤器,它可以完美地工作:

在此处输入图像描述

我也尝试了findand findOne,但没有运气。如果没有该子句,它要么不返回任何内容like,要么返回上面显示的异常。

有任何想法吗?我会永远感激不尽。

编辑 似乎数据库中的字符串与我正在比较的字符串不完全相同。但我还没有确切地知道发生了什么。这个console.log:

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

返回:

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

所以很明显有些东西使这两个字符串不完全相等,但现在我真的看不出是什么

4

1 回答 1

3

所以问题是我的函数testId直接从对象接收test,如test.id.

this id,即使它在 中显示为 a string,在console.log内部它也是一个对象,因此没有任何东西可以正确比较。

当我改变 test.id"${test.id}"一切正常。

我现在就去自杀,谢谢。

于 2021-06-01T15:53:19.670 回答