Node/Express error: TypeError: Cannot read property 'then' of undefined
Node/Express error: TypeError: Cannot read property 'then' of undefined
我向我的快递服务器提交了一个表单,但我收到以下错误:
TypeError: Cannot read property 'then' of undefined
'then' 指的是下面(截断的)代码中的 then(控制器文件中的 post 方法)。
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,
miles, color, transmission, layout, engine_type,
car_photo_url, car_price, sale_status, for_sale);
console.log(car);
car
.save()
.then(() => {
res.redirect('/');
})
.catch(err => console.log(err))
}
保存方法是模型method/my第一次尝试使用模型中的mysql2 npm 包进行交易:
save() {
db.getConnection()
.then((db) => {
return db.query('START TRANSACTION');
})
.then(() => {
db.query('INSERT INTO cars (model_year, make, model, color, miles, transmission, layout, engine_type) VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
[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()
方法应该return一个Promise,所以你可以在链中调用.then
和.catch
。
请尝试类似的操作:
save() {
return db.getConnection()
...
}
我向我的快递服务器提交了一个表单,但我收到以下错误:
TypeError: Cannot read property 'then' of undefined
'then' 指的是下面(截断的)代码中的 then(控制器文件中的 post 方法)。
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,
miles, color, transmission, layout, engine_type,
car_photo_url, car_price, sale_status, for_sale);
console.log(car);
car
.save()
.then(() => {
res.redirect('/');
})
.catch(err => console.log(err))
}
保存方法是模型method/my第一次尝试使用模型中的mysql2 npm 包进行交易:
save() {
db.getConnection()
.then((db) => {
return db.query('START TRANSACTION');
})
.then(() => {
db.query('INSERT INTO cars (model_year, make, model, color, miles, transmission, layout, engine_type) VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
[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()
方法应该return一个Promise,所以你可以在链中调用.then
和.catch
。
请尝试类似的操作:
save() {
return db.getConnection()
...
}