我向我的Express服務器提交了一個表單,我得到了以下錯誤:
'then'指的是下面(截斷的)代碼中的then(控制器檔案中的post方法)。 保存方法是一個模型方法/我第一次嘗試在模型中使用mysql2 npm包進行交易: 問題是什么? uj5u.com熱心網友回復: 請嘗試一下:
標籤:TypeError: TypeError.
TypeError。不能讀取未定義的屬性'then'exports. postAddVehicle = (req, res, next) => {
//const id = null; {
//car table
const model_year = req.body.model_year。
const make = req.body.make。
...
const car = new Car(
null, model_year, make, model,
里程,顏色,變速器,布局,發動機型別。
car_photo_url, car_price, sale_status, for_sale)。)
console.log(car)。
轎車
.save()
.then(() => {
res.redirect('/'/span>)。
})
.catch(err => console.log(err)
}
save() {
db.getConnection()
.then((db) =>/span> {
return db.query('START TRANSACTION')。
})
.then(() => {
db.query('INSERT INTO cars (model_year, make, model, color, miles, transmission, layout, engine_type) VALUES (?, ?, ?, ?, ?, ?, ?)'/span>,
[this.model_year,this.make,this. model, this.color,this。 miles, this.transmission, this。 layout, this.engine_type] )
})
.then(() => {
db.query('INSERT INTO car_photos (car_photo_url) VALUES (?) ',
[this.car_photo_url] )
})
.then(() => {db.query('INSERT INTO car_price (car_price) VALUES ( ? )', [this.car_price] )
})
.then(() => {db.query('INSERT INTO sales_status (sale_status, for_sale) VALUES ( ? , ?) ', [this.sale_status, this.for_sale] )
})
.then(() => {
return db.query('COMMIT')。
})
.catch((error) => {
return db.query('ROLLBACK')。
})
}
save()方法應該回傳一個Promise,所以你可以呼叫鏈中的.then和.catch。
save() {
return db.getConnection()
...
}
