好吧,所以我一直在尝试搜索 Mikro-ORM 的文档以查找如何传入本地 sql 查询参数,但我找不到任何东西。在玩了一点代码之后,这就是它的样子,我认为这是正确的,但我收到了这个错误
error: SELECT DISTINCT * FROM direct_messages WHERE receiver_id = $1 OR sender_id = $1 ORDER BY sent_at DESC - there is no parameter $1
at Parser.parseErrorMessage (/Users/yonden/Documents/projects/matchup/server/node_modules/pg-protocol/src/parser.ts:369:69)
at Parser.handlePacket (/Users/yonden/Documents/projects/matchup/server/node_modules/pg-protocol/src/parser.ts:188:21)
at Parser.parse (/Users/yonden/Documents/projects/matchup/server/node_modules/pg-protocol/src/parser.ts:103:30)
at Socket.<anonymous> (/Users/yonden/Documents/projects/matchup/server/node_modules/pg-protocol/src/index.ts:7:48)
at Socket.emit (events.js:400:28)
at Socket.emit (domain.js:470:12)
at addChunk (internal/streams/readable.js:290:12)
at readableAddChunk (internal/streams/readable.js:265:9)
at Socket.push (internal/streams/readable.js:204:10)
at TCP.onStreamRead (internal/stream_base_commons.js:188:23) {
length: 94,
severity: 'ERROR',
code: '42P02',
detail: undefined,
hint: undefined,
position: '60',
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'parse_expr.c',
line: '907',
routine: 'transformParamRef'
}
我的代码目前看起来像这样
@Query(() => String)
async Conversations(@Ctx() { em, userData }: MyContext): Promise<string> {
const connection = em.getConnection();
const queryString = `SELECT DISTINCT * FROM direct_messages WHERE receiver_id = $1 OR sender_id = $1 ORDER BY sent_at DESC`;
return await connection
.execute(queryString, [userData['sub']])
.then((results) => {
console.log(results);
return 'worked';
})
.catch((error) => {
console.log(error);
return 'Failed';
});
}
仅对于某些上下文 userData['sub'] 是来自 googleOAuth API 的字符串类型的用户 ID。谢谢!