我试图使用adonisjs将值插入到xampp mysql服务器中,但出现以下错误:
插入 posts
( body
, created_at
, title
, updated_at
)值(默认值,'2018-06-28 15:06:02','post 4','this is post 4','2018-06-28 15:06:02')-er\u wrong \u value \u count \u on \u row:列计数与第1行的值计数不匹配
这个错误告诉我的是,对于一个由4个值组成的表,我提供了5个值。事实上,“posts”表包含5个值,分别是:
id,标题,正文,创建时间,更新时间
我可以手动输入id,title,body的值,使用phpmyadmin,表将被更新,我的应用程序将反映更改。
我能追踪到的唯一问题似乎是postcontroller.js文件中的increments()方法。它的工作做得不对吗?
我怎样才能搞清楚这里发生了什么?
迁移文件posts\u schema.js:
'use strict'
const Schema = use('Schema')
class PostsSchema extends Schema {
up () {
this.create('posts', (table) => {
table.increments()
table.string('title')
table.string('body')
table.timestamps()
})
}
down () {
this.drop('posts')
}
}
module.exports = PostsSchema
控制器postscontroller.js:
'use strict'
// Bring in model
const Post = use('App/Models/Post')
class PostController {
async index({ view }){
// const posts = [
// {title:'Post One', body:'This is post one'},
// {title:'Post Two', body:'This is post one'},
// {title:'Post Three', body:'This is post one'}
// ]
const posts = await Post.all();
return view.render('posts.index', {
title: 'Latest Posts',
posts: posts.toJSON()
})
}
async details({ params, view}) {
const post = await Post.find(params.id)
return view.render('posts.details', {
post: post
})
}
async add({ view }) {
return view.render('posts.add')
}
async store({ request, response, session }) {
const post = new Post();
post.title = request.input('title')
post.body = request.input('body')
await post.save()
session.flash({ notification: 'Posted Success'})
return response.redirect('/posts')
}
}
module.exports = PostController
视图add.edge:
@layout('main')
@section('content')
<a href="/posts">Go Back</a>
<hr>
<h1>Add Post</h1>
<form action="/posts" method="POST">
{{ csrfField() }}
<div class="form-group">
<label>Title</label>
<input type="text" name="title" class="form-control" placeholder="Title">
</div>
<div class="form-group">
<label>Body</label>
<textarea name="title" class="form-control" placeholder="Body"></textarea>
</div>
<button class="btn btn-primary" type="submit">Submit</button>
</form>
@endsection
路由文件routes.js:
'use strict'
const Route = use('Route')
Route.on('/').render('home')
Route.get('/posts', 'PostController.index')
Route.get('/posts/add', 'PostController.add')
Route.get('/posts/:id', 'PostController.details')
Route.post('/posts', 'PostController.store')
我在配置xampp方面做得很少。我对config.inc.php的更改很简单:
$cfg['Servers'][$i]['controluser'] = 'root';
$cfg['Servers'][$i]['controlpass'] = '123456';
$cfg['Servers'][$i]['auth_type'] = 'config';
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = '12345';
/* Server parameters */
$cfg['Servers'][$i]['host'] = '127.0.0.1';
$cfg['Servers'][$i]['compress'] = false;
$cfg['Servers'][$i]['AllowNoPassword'] = true;
从开始使用adonisjs开始,我们一直在学习这段代码和教程https://www.youtube.com/watch?v=saxhwbumtu4
2条答案
按热度按时间wnvonmuf1#
如果这是您当前的插入:
应该是:
elcex8rz2#
看起来像是
name
属性应为body
而不是title
.