Sequelize Cannot read property '0' of undefine
如题.使用 create 就会报这个错误...
模型:
module.exports = function(sequelize, DataTypes) {
return sequelize.define('test_user', {
id: {
type: DataTypes.INTEGER(11).UNSIGNED.ZEROFILL,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
title: {
type: DataTypes.STRING(255),
allowNull: true
},
desc: {
type: DataTypes.STRING(255),
allowNull: true
}
}, {
tableName: 'test_user',
timestamps: false
});
};
controllers:
const User = require('../model/user')
let model= {
username:"test",
pwd: '测试测试'
}
const create = async (ctx) => {
try {
const s = await User.create(model);
ctx.body = {
code: 1000,
data: '创建成功'
}
} catch (err) {
const msg = err.errors[0]
ctx.body = {
code: 300,
data: msg.value + msg.message
}
}
}
module.exports = {
create
}
package.json:
{
"name": "koa-test-sequelize-auto",
"version": "0.1.0",
"private": true,
"scripts": {
"start": "node bin/www",
"auto": "node auto",
"dev": "./node_modules/.bin/nodemon bin/www",
"prd": "pm2 start bin/www",
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"debug": "^2.6.3",
"koa": "^2.2.0",
"koa-bodyparser": "^3.2.0",
"koa-convert": "^1.2.0",
"koa-json": "^2.0.2",
"koa-logger": "^2.0.1",
"koa-onerror": "^1.2.1",
"koa-router": "^7.1.1",
"koa-static": "^3.0.0",
"koa-views": "^5.2.1",
"mysql": "^2.16.0",
"mysql2": "^1.6.5",
"pug": "^2.0.0-rc.1",
"sequelize": "^5.2.12",
"sequelize-auto": "^0.4.29"
},
"devDependencies": {
"koa2-cors": "^2.0.6",
"nodemon": "^1.8.1"
}
}
捣鼓好久了..一直报这个错...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
sequelize throw 出的err没有errors属性
建议改成
你的模型里面是三个字段,
id
、title
、desc
调用User.create,确实
username
、pwd
这是没对上吧