如何解决 AdonisJs、XAMPP MySQL 错误

How To Troubleshoot AndonisJs, XAMMP MySQL error

我正在尝试使用 AdonisJs 将值插入我的 XAMPP MySQL 服务器,但出现以下错误:

插入 posts (body, created_at, title, updated_at) 值 (DEFAULT, '2018-06-28 15:06:02', 'post 4', 'this is post 4', '2018-06-28 15:06:02') - ER_WRONG_VALUE_COUNT_ON_ROW: 列数与第 1 行的值数不匹配

这个错误告诉我的是,我为包含 4 个值的 table 提供了 5 个值。事实上 table 'posts' 确实包含 5 个值,它们是:

id, title, body, created_at, updated_at

我可以使用 phpMyAdmin 手动将值输入 id、title、body,table 将更新,我的应用程序将反映更改。

我可以追踪到的唯一问题似乎是我的 PostController.js 文件中的 increments() 方法。它没有正确完成它的工作吗?

我如何继续弄清楚这里发生了什么?

迁移文件posts_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

如果这是您当前的插入内容:

insert into posts (body, created_at, title, updated_at) values (DEFAULT, '2018-06-28 15:06:02', 'post 4', 'this is post 4', '2018-06-28 15:06:02')

应该是:

insert into posts (body, created_at, title, updated_at) values ('this is post 4', '2018-06-28 15:06:02', 'post 4', '2018-06-28 15:06:02')
<textarea name="title" class="form-control" placeholder="Body"></textarea>

看起来 name 属性应该是 body 而不是 title