Request body is undefined

在試著打自己寫的 api 時,發現 Body 明明有以 JSON 的方式傳遞,使用 fetch function 打出去的時候,對面卻找不到Body,後來發現是兩個原因:

  1. express 少了 app.use(express.json())
  2. fetch function 的 Header 少了 "Content-Type": "application/json"

Typescript - tsconfig.json - Build:No inputs were found in config file

出現這個錯誤的解決辦法:

在 tsconfig.json 的 compilerOptions 中加上一行:

"compilerOptions": {
    "allowJs": true,
}

Go - postgresql - LastInsertId is not supported by this driver

在執行下面程式碼的時候:

sqlStatement := `INSERT INTO tx (urls) VALUES($1) RETURNING id`
res, err := db.Exec(sqlStatement, pq.Array(body.Urls))
id, err := res.LastInsertId()

出現下面錯誤: LastInsertId is not supported by this driver 這是因為 LastInsertId() 不支援 Postgresql driver

解決方式

將程式碼改為如下:

sqlStatement := `INSERT INTO tx (urls) VALUES($1) RETURNING id`
var id int
err = db.QueryRow(sqlStatement, pq.Array(body.Urls)).Scan(&id)

Typeorm Error - Cannot find module

在寫完 seed file 做執行的時候,卻出現下列錯誤:

Error: Cannot find module

這是因為在 seed.ts 內引入 entity 時的路徑關係 正確寫法:

import Flight from '../entity/Flight'

錯誤寫法:

import Flight from 'db/entity/Flight'

應使用相對路徑而非絕對路徑。

gRPC vs. WebSocket

https://ably.com/topic/grpc-vs-websocket