Sequelize可以创建数据但是不能从Models中获取数据
Sequelize can create data but can't get data from Models
我无法理解我的代码到底出了什么问题。但我知道我在实现 Promise 的方式上存在问题。作为 Shipment.findAll()
returns 一个承诺,我再次创建一个承诺供我的路由器使用。
那为什么createShipment
工作正常,而getAllShipments
不工作。
发货控制器
const Worker = require ('../models').Worker;
const Shipment = require ('../models').Shipment;
function createShipment (shipmentName, shipmentStatus) {
return new Promise ((resolve, reject) => {
Shipment.create({name: shipmentName, status: shipmentStatus})
.then (shipment => resolve(shipment))
.catch (error => reject(error));
});
}
function getAllShipments () {
return new Promise ((resolve, reject) => {
Shipment.findAll()
.then(allShipments => {
console.log(allShipments);
return resolve(allShipments);
})
.catch(error => reject(error))
})
}
module.exports = {
createShipment,
getAllShipments
}
货运路由器
var router = require('express').Router();
var Shipment = require('./../../../controllers/shipment');
router.post ('/' , (req,res) => {
Shipment.createShipment ('New Shipment', 'Pending')
.then (shipment => {
res.status(200).json({status: true, data: shipment, errors: null, msg: "Shipment Added Successfully"});
})
.catch (error => {
res.status(200).json({status: false, data: {}, errors: error, msg: "Error Creating Shipment. Please see details in Errors Object"});
});
});
router.get('/' , (req, res) => {
Shipment.getAllShipments()
.then(allShipments => {
return res.status(200).status({status: true, data: allShipments, errors: null, msg: "All Shipments fetched successfully"});
})
.catch(error => {
return res.status(200).json({status: false, data: {}, errors: error, msg: "Error Fetching Shipments. Please see details in Errors Object"});
});
})
module.exports = router;
我做错了什么?因为 getAllShipments
在控制台上给出了我的输出,但是路由没有发送响应,只是在等待。
变化:
Shipment.getAllShipments()
.then(allShipments => {
return res.status(200).status({status: true, data: allShipments, errors: null, msg: "All Shipments fetched successfully"});
})
至:
return res.status(200).json({status: true, data: allShipments, errors: null, msg: "All Shipments fetched successfully"});
只是一个错字,你写了两次状态而不是 json
我无法理解我的代码到底出了什么问题。但我知道我在实现 Promise 的方式上存在问题。作为 Shipment.findAll()
returns 一个承诺,我再次创建一个承诺供我的路由器使用。
那为什么createShipment
工作正常,而getAllShipments
不工作。
发货控制器
const Worker = require ('../models').Worker;
const Shipment = require ('../models').Shipment;
function createShipment (shipmentName, shipmentStatus) {
return new Promise ((resolve, reject) => {
Shipment.create({name: shipmentName, status: shipmentStatus})
.then (shipment => resolve(shipment))
.catch (error => reject(error));
});
}
function getAllShipments () {
return new Promise ((resolve, reject) => {
Shipment.findAll()
.then(allShipments => {
console.log(allShipments);
return resolve(allShipments);
})
.catch(error => reject(error))
})
}
module.exports = {
createShipment,
getAllShipments
}
货运路由器
var router = require('express').Router();
var Shipment = require('./../../../controllers/shipment');
router.post ('/' , (req,res) => {
Shipment.createShipment ('New Shipment', 'Pending')
.then (shipment => {
res.status(200).json({status: true, data: shipment, errors: null, msg: "Shipment Added Successfully"});
})
.catch (error => {
res.status(200).json({status: false, data: {}, errors: error, msg: "Error Creating Shipment. Please see details in Errors Object"});
});
});
router.get('/' , (req, res) => {
Shipment.getAllShipments()
.then(allShipments => {
return res.status(200).status({status: true, data: allShipments, errors: null, msg: "All Shipments fetched successfully"});
})
.catch(error => {
return res.status(200).json({status: false, data: {}, errors: error, msg: "Error Fetching Shipments. Please see details in Errors Object"});
});
})
module.exports = router;
我做错了什么?因为 getAllShipments
在控制台上给出了我的输出,但是路由没有发送响应,只是在等待。
变化:
Shipment.getAllShipments()
.then(allShipments => {
return res.status(200).status({status: true, data: allShipments, errors: null, msg: "All Shipments fetched successfully"});
})
至:
return res.status(200).json({status: true, data: allShipments, errors: null, msg: "All Shipments fetched successfully"});
只是一个错字,你写了两次状态而不是 json