我有一个 Express API,用于使用 Knex 和 ObjectionJS Orm 创建数据订单。当我尝试调用时,API 处于挂起状态,没有任何错误
所以我尝试在我的其他计算机上运行它,然后它运行良好。我不知道发生了什么。数据库版本相同
我的环境:
mysql: 5.7.37 (running on docker)
node: v14.18.2
包.json:
"dependencies": {
"app-root-path": "^3.0.0",
"axios": "^0.21.1",
"bcrypt": "^5.0.0",
"bull": "^4.2.1",
"cloudinary": "^1.21.0",
"cors": "^2.8.5",
"crypto": "^1.0.1",
"csv-parse": "^4.12.0",
"datauri": "^2.0.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-validator": "^6.4.0",
"googleapis": "^92.0.0",
"jsonwebtoken": "^8.5.1",
"knex": "^1.0.2",
"multer": "^1.4.2",
"mysql": "^2.18.1",
"mysql2": "^2.3.2",
"node-sass": "^5.0.0",
"nodemailer": "^6.5.0",
"objection": "^3.0.1",
"randomstring": "^1.1.5",
"react-feather": "^2.0.9",
"reactstrap": "^8.9.0",
"socket.io": "^2.3.0",
"supertest": "^4.0.2",
"uuid": "^8.3.1",
"winston": "^3.2.1",
"xendit-node": "^1.12.0"
},
复制代码:
createOrder: async (req, res) => {
// Starting Order Transaction
try {
console.debug("Getting req body")
const { service_id, order_qty, data } = req.body
const notes = req.body.notes || ""
// Find Service
console.debug("Find service")
const findService = await ServiceModel.query()
.withGraphFetched("[serviceType]")
.findById(service_id)
// Throw Error if Service is not found
console.debug("Throw Error if Service is not found")
if (!findService) {
return Response.notFound(res, "Service not found")
}
// User
console.debug("Get user data")
const user = await Auth.user(req)
const generateCode = new Date().getTime().toString(36)
// Generate Order Code
console.debug("Generate code")
const order_code = `ORDER-${generateCode}`.toLocaleUpperCase()
// Calculate Total Service By = Service Price x Quantity
const total_price = findService.price * order_qty
// Create new sheets
console.debug("Create new sheets")
const sheet = await Sheets.generateEmptySheet(
`Laporan: ${order_code}`
)
// Try Insert Order to DB
console.debug("Try Insert Order to DB") // Pending happen in here
const createOrder = await OrderModel.query().insert({
order_code,
service_id,
order_qty,
notes,
total_price,
data,
order_status: 2,
user_id: user.id,
order_report: sheet.spreadsheetId,
})
// Throw Error if Created order failed
if (!createOrder) {
return Response.error(res, "Something went wrong")
}
// Check User Credit Balance
const checkUserBalance = user.credit >= total_price
if (checkUserBalance) {
// Update User Balance
const updateUser = await UserModel.query()
.findById(user.id)
.patch({
credit: user.credit - total_price,
})
// Throw Error if Update User Credit
if (!updateUser) {
return Response.error(res, "Update User Credit Not Working")
}
} else {
return Response.error(res, "insufficient balance")
}
// Insert to Order History
await OrderHistoryModel.query().insert({
order_id: createOrder.id,
order_status: 0,
order_message: `Pesanan dengan code ${order_code} telah masuk`,
})
// Create new task
await Worker.createTask(
createOrder.id,
findService.event_worker_name,
order_qty,
data,
findService.service_external_id || "",
sheet.spreadsheetId
)
return Response.success(res, { message: "OKE" })
} catch (e) {
log.error(e.message)
return Response.error(res, e.message)
}
},
===[已编辑]===
我从 mysql 数据库获取日志:
Version: '5.7.37' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL)
2022-02-02T12:42:10.463339Z 3 [Note] Aborted connection 3 to db: 'my_project' user: 'root' host: '172.21.0.1' (Got an error reading communication packets)
2022-02-02T12:48:43.537964Z 19 [Note] Aborted connection 19 to db: 'my_project' user: 'root' host: '172.21.0.1' (Got an error reading communication packets)
2022-02-02T12:48:43.538010Z 18 [Note] Aborted connection 18 to db: 'my_project' user: 'root' host: '172.21.0.1' (Got an error reading communication packets)
2022-02-02T12:49:14.809745Z 29 [Note] Aborted connection 29 to db: 'my_project' user: 'root' host: '172.21.0.1' (Got an error reading communication packets)
2022-02-02T12:49:14.809968Z 28 [Note] Aborted connection 28 to db: 'my_project' user: 'root' host: '172.21.0.1' (Got an error reading communication packets)
2022-02-02T12:49:35.070066Z 31 [Note] Aborted connection 31 to db: 'my_project' user: 'root' host: '172.21.0.1' (Got an error reading communication packets)
2022-02-02T12:49:35.071208Z 30 [Note] Aborted connection 30 to db: 'my_project' user: 'root' host: '172.21.0.1' (Got an error reading communication packets)
2022-02-02T12:55:30.761014Z 32 [Note] Aborted connection 32 to db: 'my_project' user: 'root' host: '172.21.0.1' (Got an error reading communication packets)
2022-02-02T12:56:20.393102Z 33 [Note] Aborted connection 33 to db: 'my_project' user: 'root' host: '172.21.0.1' (Got an error reading communication packets)