我在 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
有趣的是,如果我去环回浏览器和过滤器,它可以完美地工作:
我也尝试了find
and 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
所以很明显有些东西使这两个字符串不完全相等,但现在我真的看不出是什么。