Итак, я собираюсь с драйвером mysql2 использовать Mysql с node. Скажем, у меня от 25 до 30, может быть, больше полей в строке, мне придется вручную писать каждый столбец после извлечения его из тела. Мой текущий подход такой, как показано ниже, вот полный контроллер для лучшего понимания.
exports.registerUser = async (req, res, next) => {
let errors = validationResult(req)
if (!errors.isEmpty()) {
let err = new Error()
err.errors = errors
err.status = 400
next(err)
}
console.log('entered')
let { user_role_id, name, email_id, password,
mobile_no, work_exp_years, work_exp_month,
resume, current_location } = req.body
try {
let query = `INSERT INTO user_account_details
(user_role_id, name, email_id, password,
mobile_no, work_exp_years, work_exp_month,
resume, current_location) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`
let [insertRow] = await mysql.execute(query, [user_role_id, name, email_id, password,
mobile_no, work_exp_years, work_exp_month,
resume, current_location])
if (!insertRow) {
throw new Error('Insert error')
}
return res.status(200).json({ result: insertRow })
} catch (err) {
let error = new Error()
error.errors = err
next(error)
}
}
Итак, меня беспокоит только та часть, где я вставляю поля в строку, я чувствую, что пишу имена столбцов a много раз, есть ли способ просто взять тело и вставить его в строку, как в пн goose?