为什么在调用数据库呼叫时未定义我的论点?
我正在尝试通过GraphQL解析器返回Postgres DB查询。出于某种原因,结果
在我(故意)查询不存在的ID时未定义的返回。当我安装它记录它时,结果
总是按预期返回:
result = {
rows: [],
...
}
但是,当我运行并进行查询时,或者当我使用Node Inspect Instex.js
时,请让它运行直到调试器为止,它似乎还没有确定结果
。当我运行console.log(result)
击中调试器时,我会遇到此错误:
debug> console.log(result)
REPL2:1
console.log(result)
^
Uncaught ReferenceError: result is not defined
at REPL2:1:13
at Script.runInContext (node:vm:139:12)
at Object.runInContext (node:vm:289:6)
at REPLServer.controlEval (node:internal/debugger/inspect_repl:578:25)
at bound (node:domain:421:15)
at REPLServer.runBound [as eval] (node:domain:432:12)
at REPLServer.onLine (node:repl:898:10)
at REPLServer.emit (node:events:527:28)
at REPLServer.emit (node:domain:475:12)
at [_onLine] [as _onLine] (node:internal/readline/interface:424:12)
当我从操场上运行查询时,我会得到此错误:
/Users/user/Projects/geolocatr_api/db/queries/user.queries.js:21
id: user.id,
^
TypeError: Cannot read properties of undefined (reading 'id')
at /Users/user/Projects/geolocatr_api/db/queries/user.queries.js:21:20
at Query.<anonymous> (/Users/user/Projects/geolocatr_api/node_modules/pg-pool/index.js:417:18)
at Query.handleReadyForQuery (/Users/user/Projects/geolocatr_api/node_modules/pg/lib/query.js:138:12)
at Client._handleReadyForQuery (/Users/user/Projects/geolocatr_api/node_modules/pg/lib/client.js:290:19)
at Connection.emit (node:events:527:28)
at /Users/user/Projects/geolocatr_api/node_modules/pg/lib/connection.js:114:12
at Parser.parse (/Users/user/Projects/geolocatr_api/node_modules/pg-protocol/dist/parser.js:40:17)
at Socket.<anonymous> (/Users/user/Projects/geolocatr_api/node_modules/pg-protocol/dist/index.js:11:42)
at Socket.emit (node:events:527:28)
at addChunk (node:internal/streams/readable:324:12)
Node.js v18.0.0
error Command failed with exit code 1.
我真的不确定为什么我的控制台日志似乎看到了某些东西,但是节点却没有。否则,当将表格中存在的ID传递时,它可以按预期工作。
代码:
// Resolver
Query: {
user: ( _, { id } ) => {
console.log({id}); // Node recognizes this as the correct number
return getUserById(id);
},
},
// DB Query Function
const getUserById = (id) => {
return new Promise(function(resolve, reject) {
db.query(
`
SELECT id, first_name, email, phone
FROM users
WHERE id = $1;
`,
[id],
function(err, result) {
// debugger; // Result comes back as not defined
if (!result.rows) { // Will not run this if block
console.log(result.rows);
reject("Could not find user.");
}
if (err) reject(err);
const user = rows[0];
resolve({
id: user.id,
firstName: user.first_name,
email: user.email,
phone: user.phone,
});
}
);
});
};
I am using trying to return a Postgres DB query via a GraphQL resolver. For some reason, result
is coming back as not defined when I (on purpose) query a row for an ID that doesn't exist. When I console log it, result
always comes back as expected:
result = {
rows: [],
...
}
However, when I run it and make a Query, or when I use node inspect index.js
and let it run until the debugger, it doesn't seem to recognize that result
is even defined. When I run console.log(result)
in Node when it hits the debugger, I get this error:
debug> console.log(result)
REPL2:1
console.log(result)
^
Uncaught ReferenceError: result is not defined
at REPL2:1:13
at Script.runInContext (node:vm:139:12)
at Object.runInContext (node:vm:289:6)
at REPLServer.controlEval (node:internal/debugger/inspect_repl:578:25)
at bound (node:domain:421:15)
at REPLServer.runBound [as eval] (node:domain:432:12)
at REPLServer.onLine (node:repl:898:10)
at REPLServer.emit (node:events:527:28)
at REPLServer.emit (node:domain:475:12)
at [_onLine] [as _onLine] (node:internal/readline/interface:424:12)
and when I run the query from the Playground, I get this error:
/Users/user/Projects/geolocatr_api/db/queries/user.queries.js:21
id: user.id,
^
TypeError: Cannot read properties of undefined (reading 'id')
at /Users/user/Projects/geolocatr_api/db/queries/user.queries.js:21:20
at Query.<anonymous> (/Users/user/Projects/geolocatr_api/node_modules/pg-pool/index.js:417:18)
at Query.handleReadyForQuery (/Users/user/Projects/geolocatr_api/node_modules/pg/lib/query.js:138:12)
at Client._handleReadyForQuery (/Users/user/Projects/geolocatr_api/node_modules/pg/lib/client.js:290:19)
at Connection.emit (node:events:527:28)
at /Users/user/Projects/geolocatr_api/node_modules/pg/lib/connection.js:114:12
at Parser.parse (/Users/user/Projects/geolocatr_api/node_modules/pg-protocol/dist/parser.js:40:17)
at Socket.<anonymous> (/Users/user/Projects/geolocatr_api/node_modules/pg-protocol/dist/index.js:11:42)
at Socket.emit (node:events:527:28)
at addChunk (node:internal/streams/readable:324:12)
Node.js v18.0.0
error Command failed with exit code 1.
I'm really not sure why my console logs seem to see that something is there, but Node doesn't. Otherwise, when an ID that exists in the table is passed in, it works as expected.
The code:
// Resolver
Query: {
user: ( _, { id } ) => {
console.log({id}); // Node recognizes this as the correct number
return getUserById(id);
},
},
// DB Query Function
const getUserById = (id) => {
return new Promise(function(resolve, reject) {
db.query(
`
SELECT id, first_name, email, phone
FROM users
WHERE id = $1;
`,
[id],
function(err, result) {
// debugger; // Result comes back as not defined
if (!result.rows) { // Will not run this if block
console.log(result.rows);
reject("Could not find user.");
}
if (err) reject(err);
const user = rows[0];
resolve({
id: user.id,
firstName: user.first_name,
email: user.email,
phone: user.phone,
});
}
);
});
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常使用这些
(err,结果)=&gt; {...}
回调,您检查err
first ,因为如果是真实,第二个参数很有可能不会定义。您还应
返回
在检测到错误以防止其余回调执行后。fyi, node-postgresl node-postgresl 在本地支持承诺,因此无需明确创建一个。在下面,我将您的代码转换为异步函数,以利用这一点。
Typically with those
(err, result) => { ... }
callbacks, you checkerr
first because if it is truthy, there's a good chance that the second argument will not be defined.You should also
return
after detecting an error to prevent the rest of your callback from executing.FYI, node-postgresl supports promises natively so there's no need to explicitly create one. Below I've translated your code to an async function to take advantage of this.