问题描述
我在 Loopback 3 中有以下模型:
{
"name": "rehearsalTest","base": "PersistedModel","idInjection": true,"options": {
"validateUpsert": true
},"properties": {
"creationDate": {
"type": "date"
},"userId": {
"type": "string","required": true
},"courseId": {
"type": "string","templateId": {
"type": "string","testId": {
"type": "string","grade": {
"type": "string","required": false
}
},"validations": [],"relations": {},"acls": [
{
"accesstype": "*","principalType": "ROLE","principalId": "$unauthenticated","permission": "DENY"
},{
"accesstype": "*","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}},data);
});
抛出这个异常:
Error: The testId property has invalid clause {"like":"60afbc93f30a9f7807fc95d1"}: Expected a string or RegExp
有趣的是,如果我转到环回资源管理器和过滤器,它可以完美运行:
我也尝试了 find
和 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
很明显,有些东西使这两个字符串不完全相等,但现在我真的看不到什么。
解决方法
所以问题是我的函数直接从对象 testId
接收 test
,作为 test.id
。
这个 id
,即使它在 string
中显示为 console.log
,但在内部它是一个对象,因此没有任何比较正确。
当我用 "${test.id}"
更改 test.id 时,一切正常。
我现在就自杀,谢谢。