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条)

报告相同问题?

悬赏问题

  • ¥20 qt中connect两个signal
  • ¥20 pix2pixHD运行测试命令时出现数据类型错误无法反向传播的问题
  • ¥15 python处理Excel符合条件的行自动填写数据分类
  • ¥15 汇编hook举例并讲解(通俗易懂,学习用)
  • ¥20 用c++语言模拟键盘电子琴设计
  • ¥15 STM32cubemx生成keil工程,有问题与正常的情况不同,求解!
  • ¥15 如何自动点击银行app的安全键盘,实现密码自动输入
  • ¥15 关于四边形重叠的问题
  • ¥15 用verilog语言设计一个简易的八音符电子琴,可通过按键输入来控制音响。演奏时可以选择是手演奏(由键盘输入)或自动演奏已存入的乐曲。能够自动演奏多首乐曲,且每首乐曲可重复演奏
  • ¥15 sap gui脚本每次到导出Excel的时候就停住不动。不会另存为。