我有一個node.js/express應用程序使用node-postgres模塊與Postgres數據庫進行通信。它正在處理異步/等待,但是如果我將對象重命名爲{rows}以外的任何其他對象,則會返回undefined。這是我的代碼;注意右下async
和註釋,上面const { rows } = ...
:爲什麼我不能在我的節點應用程序中重命名這個對象?
var express = require('express');
var router = express.Router();
const { Pool } = require('pg');
const pool = new Pool({
connectionString: 'postgresql://[email protected]/mydb'
});
router.post('/login', function(req, res, next) {
var username = req.body.username;
var password = req.body.password;
(async() => {
// if I rename this from "rows" to, say, "userdetails", it comes back undefined
const { rows } = await pool.query(`
SELECT id, password
FROM myschema.users
WHERE username = $1
LIMIT 1;`,[username]);
if (rows.length) {
// check password, etc, etc.
return res.status(200).send();
} else {
return res.status(401).send();
}
})().catch(e => setImmediate(() => {
res.status(500);
}
));
});
module.exports = router;
我敢肯定,我想的東西很基本在這裏,但我爲什麼不能重命名此? pg模塊是否指定返回的var/const必須命名爲{ rows }
?如果是這樣,我怎麼會發現?我在await
處設置了一個斷點並逐步完成代碼,但對我而言仍然不清楚。
以防萬一,你重命名的所有實例行變量到你的新變量名? – Alan