doujiao8491 2018-06-28 08:55
浏览 91
已采纳

如何解决AdonisJs,XAMPP MySQL错误

I'm trying to insert values into my XAMPP MySQL server using AdonisJs, but getting the following error:

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') - ER_WRONG_VALUE_COUNT_ON_ROW: Column count doesn't match value count at row 1

What this error tells me is that I am providing 5 values, for a table that consists of 4 values. In fact the table 'posts' does consist of 5 values which are:

id, title, body, created_at, updated_at

I can manually input values into id, title, body, using phpMyAdmin, the table will be updated, and my application will reflect the change.

The only issue I can track this to, seems to be with the increments() method in my PostController.js file. Is it not doing its job correctly?

How do I proceed to figure out whats going on here?

Migration file 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

controller 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

The view 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

The routes file 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')

I have done very little to configure XAMPP. My changes to config.inc.php were simply:

$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;

been working through this code and tutorial from: Getting Started with AdonisJs https://www.youtube.com/watch?v=SaXhwbuMTu4

  • 写回答

2条回答 默认 最新

  • dpkk8687 2018-06-30 05:27
    关注
    <textarea name="title" class="form-control" placeholder="Body"></textarea>
    

    It looks like the name attribute should be body rather than title.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵