router.get('/city/:id',CityController.get);
const get = async(req,res)=>{
try{
const response = cityService.getCity(req.params.id);
return res.status(StatusCodes.OK).json({
data:response,
success:true,
message:"Successfully Fetched a city",
error:{},
});
}catch(error){
console.log(error);
return res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({
data:{},
success:false,
message:"Not able to get the city",
error:error,
});
}
}
async getCity(id){
try{
const city = await this.cityRepository.getCity(id);
return city;
}catch(error){
console.log("Something went wrong at service layer");
throw error;
}
}
async getCity(id) {
try {
const city = await City.findByPk(id);
return city;
} catch (error) {
console.log(error);
throw {error};
}
}
** 这是从城市获取数据的实现逻辑的一个问题是它在邮递员的响应中显示** { "data": {}, "success": true, "message": "Successfully Fetched a city", "error": {} }
这是城市模型
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class City extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
}
}
City.init({
name: DataTypes.STRING,
unique:true,
allowNull: false,
}, {
sequelize,
modelName: 'City',
});
return City;
};
这就是迁移
'use strict';
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('Cities', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING,
allowNull:false,
unique:true,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('Cities');
}
};
** 但我在终端中出现错误并且也没有获取数据
我尝试过检查控制台问题出在哪里,还检查我已创建 id 作为主键的数据库**
这是我的问题,如果有人可以提出问题是什么?