Mikro-ORM: error: there is no parameter $1
Mikro-ORM: error: there is no parameter $1
好吧,我一直在尝试搜索 Mikro-ORM 的文档以查找如何传入本机 sql 查询参数,但我一直无法找到任何内容。在稍微玩了一下代码之后,这就是它的样子,我认为这是正确的,但我收到了这个错误
error: SELECT DISTINCT * FROM direct_messages WHERE receiver_id = OR sender_id = ORDER BY sent_at DESC - there is no parameter
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 = OR sender_id = 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。谢谢!
您需要在原始查询中使用 ?
而不是 </code>。这就是底层查询构建器 knex 的工作原理。</p>
<pre><code>const connection = em.getConnection();
const queryString = `SELECT DISTINCT * FROM direct_messages WHERE receiver_id = ? OR sender_id = ? ORDER BY sent_at DESC`;
return await connection
.execute(queryString, [userData['sub'], userData['sub']])
或者你可以使用 knex 的命名绑定,这是类似的(允许在查询中多次使用一个参数):
// the `em` needs to be typed to `SqlEntityManager` to have the `getKnex` method
const knex = em.getKnex();
const qb = knex.raw('SELECT DISTINCT * FROM direct_messages WHERE receiver_id = :id OR sender_id = :id ORDER BY sent_at DESC', { id: userData['sub'] });
const res = await em.execute(qb);
// ...
或者 MikroORM 查询构建器有 qb.raw()
方法,它只是 em.getKnex().raw()
:
的快捷方式
https://github.com/mikro-orm/mikro-orm/blob/master/tests/QueryBuilder.test.ts#L1314-L1319
好吧,我一直在尝试搜索 Mikro-ORM 的文档以查找如何传入本机 sql 查询参数,但我一直无法找到任何内容。在稍微玩了一下代码之后,这就是它的样子,我认为这是正确的,但我收到了这个错误
error: SELECT DISTINCT * FROM direct_messages WHERE receiver_id = OR sender_id = ORDER BY sent_at DESC - there is no parameter
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 = OR sender_id = 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。谢谢!
您需要在原始查询中使用 ?
而不是 </code>。这就是底层查询构建器 knex 的工作原理。</p>
<pre><code>const connection = em.getConnection();
const queryString = `SELECT DISTINCT * FROM direct_messages WHERE receiver_id = ? OR sender_id = ? ORDER BY sent_at DESC`;
return await connection
.execute(queryString, [userData['sub'], userData['sub']])
或者你可以使用 knex 的命名绑定,这是类似的(允许在查询中多次使用一个参数):
// the `em` needs to be typed to `SqlEntityManager` to have the `getKnex` method
const knex = em.getKnex();
const qb = knex.raw('SELECT DISTINCT * FROM direct_messages WHERE receiver_id = :id OR sender_id = :id ORDER BY sent_at DESC', { id: userData['sub'] });
const res = await em.execute(qb);
// ...
或者 MikroORM 查询构建器有 qb.raw()
方法,它只是 em.getKnex().raw()
:
https://github.com/mikro-orm/mikro-orm/blob/master/tests/QueryBuilder.test.ts#L1314-L1319