的指南進行的。
并且:
然后拋出錯誤:
<! DOCTYPE html> < html lang="en"> < head> <meta charset="utf-8"/span>> <title> Error< title> </head> <body> <pre> 無法PUT /merchants</pre> < <body> </html>
這只不過是html中的這個錯誤資訊(我只需要在這里將其格式化為markdown引文就可以得到這個):
錯誤Cannot PUT /merchants
當我運行沒有引數的UPDATE時,這個錯誤保持不變:
... 'UPDATE merchants SET name = "test" WHERE id = 2', [id, name] ...。
我已經嘗試從PUT方法切換到POST,但隨后每一個UPDATE只是添加了一個新的商家,有一個新的id和一個null值的email,見上面的截圖。SQL中的記錄并沒有被UPDATE命令所更新,你可以在實時的第一行中看到表中的變化。
我應該怎么做才能讓UPDATE發揮作用?
UPDATE:
正如答案 評論所顯示的,使用""做一個欄位。但這并不能解決問題,我把代碼改為'UPDATE merchants SET name = 2 WHERE id = 1',因為引數不需要引號,我在 "node.js "終端得到了錯誤,停止了 "node.js "服務:
<
my_user@MY_PC:~/my_project/node-postgres$ node index.js
App在埠3001.上運行。
/home/my_user/my_project/node-postgres/merchant_model.js: 61
resolve(`A merchant has been updated: ${JSON.stringify(results.rows[0])}`)
^
TypeError: 不能讀取屬性'rows' of undefined。
at pool.query (/home/my_user/my_project/node-postgres/merchant_model. js:61:70)
在Query.client。 query [as callback] (/home/my_user/my_project/node-postgres/node_modules/pg-pool/index. js:387:18)
在Query.handleError(/home/my_user/my_project/node-postgres/node_modules/pg/lib/query. js:128:19)
在Client._handleErrorMessage(/home/my_user/my_project/node-postgres/node_modules/pg/lib/client. js:335:17)
在Connection.emit(events.js:198:13)。
at parse (/home/my_user/my_project/node-postgres/node_modules/pg/lib/connection.js: 114:12)
在Parser.parse(/home/my_user/my_project/node-postgres/node_modules/pg-protocol/dist/parser. js:40:17)
在Socket.stream.on(/home/my_user/my_project/node-postgres/node_modules/pg-protocol/dist/index. js:11:42)
at Socket.emit(events.js:198:13)。
at addChunk (_stream_readable.js:288:12)
更新2:
。
正如評論中所要求的,我打開了這個測驗資料庫的日志。下面是作業前的最后一次查詢:
20210920 23: 33: 09. 071 CEST [2637] my_user@my_database LOG: statement: SELECT * FROM merchants ORDER BY id ASC
2021-09-20 23:33:44. 346 CEST [2641] my_user@my_database ERROR: bind message supplies 2 parameters, but prepared statement "" requires 0.
2021-09-20 23:33:44. 346 CEST [2641] my_user@my_database STATEMENT: UPDATE商人SET name = 'test' WHERE id = 2
2021-09-20 23:37:37。 465 CEST [2674] my_user@my_database LOG: statement: SELECT * FROM merchants ORDER BY id ASC
2021-09-20 23:37:38。 586 CEST [2674] my_user@my_database LOG: statement: SELECT * FROM merchants ORDER BY id ASC
2021-09-20 23:37:46. 853 CEST [2674] my_user@my_database ERROR: bind message supplies 2 parameters, but prepared statement "" requires 1.
2021-09-20 23:37:46. 853 CEST [2674] my_user@my_database STATEMENT: UPDATE商人SET name = '$2' WHERE id = $ 1
2021-09-20 23:41:16。 399 CEST [2704] my_user@my_database LOG: statement: SELECT * FROM merchants ORDER BY id ASC
2021-09-20 23:41:31. 879 CEST [2706] my_user@my_database ERROR: bind message supplies 2 parameters, but prepared statement " " requires 1.
uj5u.com熱心網友回復:
問題可能出在陳述句'UPDATE merchants SET name = "2" WHERE id = 1'
在Postgres中,雙引號指的是一個欄位名,而不是一個值。你需要使用單引號。
uj5u.com熱心網友回復:
有很多錯誤的步驟導致了這個問題。
我復制了createMerchant函式的代碼,但忘記了改變
resolve(`A merchant has been updated: ${JSON.stringify(results.rows[0])}`)
如果我只是更新一些東西,results.rows是空的,它們只有在你創建一個新行時才有一個值。現在它可以用:
const updateMerchant=(body)=> {
return new Promise(function(resolve, reject) {
const { id, name } = body
pool.query("UPDATE merchants SET name = 2 WHERE id = 1 RETURNING *", [id, name], (error, results) => /span> {
if (error) {
reject(error)
}
resolve(`Merchant updated with ID: ${id}, changed name to ${name}. `)
})
})
}
- 但是當我現在拿著錯誤的
results.rows的舊版本和錯誤的版本時,我也得到了同樣的錯誤,即使有正確的引號:
/home/my_user/my_project/node-postgres/merchant_model.js:61
resolve(`A merchant has been updated: ${JSON.stringify(results.rows[0])}`)
^
TypeError: 不能讀取屬性'rows' of undefined。
at pool.query (/home/my_user/my_project/node-postgres/merchant_model. js:61:70)
在Query.client。 query [as callback] (/home/my_user/my_project/node-postgres/node_modules/pg-pool/index. js:387:18)
在Query.handleError(/home/my_user/my_project/node-postgres/node_modules/pg/lib/query. js:128:19)
在Client._handleErrorMessage(/home/my_user/my_project/node-postgres/node_modules/pg/lib/client. js:335:17)
在Connection.emit(events.js:198:13)。
at parse (/home/my_user/my_project/node-postgres/node_modules/pg/lib/connection.js: 114:12)
在Parser.parse(/home/my_user/my_project/node-postgres/node_modules/pg-protocol/dist/parser. js:40:17)
在Socket.stream.on(/home/my_user/my_project/node-postgres/node_modules/pg-protocol/dist/index. js:11:42)
at Socket.emit(events.js:198:13)。
at addChunk (_stream_readable.js:288:12)
問題是,我經常不重啟node.js,而只重啟reactJS。當我在node.js后端時,我習慣于獲得所有js檔案的實時更新,但在前端,這當然是不一樣的!我需要重新啟動node.js。你需要重啟node.js來應用前臺的變化。當重啟node.js時,作為前端的 "React App "可以進一步運行,并將在再次運行時找到后端。
只有當我重新啟動 node.js 時,我才得到了新設定的真正錯誤,包括錯誤的""引號。
因此,最初的錯誤
。Cannot PUT /merchant可能是今天專案的一個稍舊的版本的東西,在那里我試驗了在fetch地址中添加引數,如fetch(http://localhost:3001/merchants/${id}`, {`和其他測驗。我缺乏膽量和時間來重現這個問題。也許,它可能仍然被問題底部的日志輸出所覆寫,就在它最終作業之前,說:2021-09-20 23:33:44。 346 CEST [2641] my_user@my_database ERROR: 系結資訊提供了2個引數,但準備好的陳述句"" 要求 0 。
至少,現在已經解決了,在第一個作業的UPDATE查詢的日志輸出:
20210920 23: 47: 51. 648 CEST [2792] my_user@my_database LOG: execute </span>unnamed>。UPDATE商人 SET name = $2 WHERE id = operator">= $1 RETURNING * 2021-09-20 23: 47: 51. 648 CEST [2792] my_user@my_database DETAIL: 引數。$1 = '2'/span>。$2 = 'asdf' 2021-09-20 23: 48: 03. 374 CEST [2795] my_user@my_database LOG: statement: SELECT * FROM merchants ORDER BY id ASC
示例截圖,更新ID 2:
按F5:
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/322220.html
標籤:
上一篇:R中的"|"是什么意思?
下一篇:單元連接的Python陣列






