NodeJs Ajax CRUD jsgrid

时间:2017-12-01 09:06:31

标签: node.js ajax mongodb jsgrid

当我在视图中单击“删除”按钮时出现一个错误。在过去它的工作成功,现在我有一个问题。

  

Erreur:   events.js:160

 throw er; // Unhandled 'error' event
  ^
     

TypeError:callback.apply不是函数       在立即。 (D:\ jsgrid- expressmaster \ node_modules \ mongoose \ lib \ model.js:4046:16)

     

立刻。 (d:\ jsgrid快车主\ node_modules \ mquery \ lib中\ utils.js:137:16)

     

在runCallback(timers.js:672:20)

     

在tryOnImmediate(timers.js:645:5)

     

at processImmediate [as _immediateCallback](timers.js:617:5)

 ^

var express = require('express');
var router = express.Router();

var Categorie = require('../models/user');  //Categorie


var getClientFilter = function(query) {
    var result = {
        Name: new RegExp(query.Name, "i"),
        Address: new RegExp(query.Address, "i")
    };
    if(query.Married) {
        result.Married = query.Married === 'true' ? true : false;
    }
    if(query.Country && query.Country !== '0') {
        result.Country = parseInt(query.Country, 10);
    }
    return result;
};
var prepareItem = function(source) {
    var result = source;
    result.Married = source.Married === 'true' ? true : false;
    result.Country = parseInt(source.Country, 10);
    return result;
};

router.get('/', function(req, res, next) {
    Categorie.find(getClientFilter(req.query), function(err, items) {
        res.json(items);
    });
});
router.post('/', function(req, res) {
    Categorie.create(prepareItem(req.body), function(err, item) {
        res.json(item);
    });
});
router.put('/', function(req, res, next) {
    var item = prepareItem(req.body);
    Categorie.update({ _id: item._id }, item, {}, function(err, item) {
        res.json(item);
    });
});
router.delete('/', function(req) {
    var item = prepareItem(req.body);
    Categorie.remove({ _id: item._id }, {}, function(err, item) {
        res.json(item);
    });
});

module.exports = router;
extends layout

block head
  link(href='https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid.min.css', rel='stylesheet')
  link(href='https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid-theme.min.css', rel='stylesheet')

block content
  div#jsGrid

block scripts
  script(src='https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid.min.js')
  script(src='/js/sample.js')

$(function() {

    var countries = [
        { Name: "", Id: 0 },
        { Name: "United States", Id: 1 },
        { Name: "Canada", Id: 2 },
        { Name: "United Kingdom", Id: 3 },
        { Name: "France", Id: 4 },
        { Name: "Brazil", Id: 5 },
        { Name: "China", Id: 6 },
        { Name: "Russia", Id: 7 }
    ];

    $("#jsGrid").jsGrid({
        height: "70%",
        width: "100%",
        filtering: true,
        inserting: true,
        editing: true,
        sorting: true,
        paging: true,
        autoload: true,
        pageSize: 5,
        pageButtonCount: 5,
        deleteConfirm: "Do you really want to delete client?",
        controller: {
            loadData: function(filter) {
                return $.ajax({
                    type: "GET",
                    url: "/clients",
                    data: filter
                });
            },
            insertItem: function(item) {
                return $.ajax({
                    type: "POST",
                    url: "/clients",
                    data: item
                });
            },
            updateItem: function(item) {
                return $.ajax({
                    type: "PUT",
                    url: "/clients",
                    data: item
                });
            },
            deleteItem: function(item) {
                return $.ajax({
                    type: "DELETE",
                    url: "/clients",
                    data: item
                });
            }
        },
        fields: [
            { name: "Name", type: "text", width: 150 },
            { name: "Age", type: "number", width: 50, filtering: false },
            { name: "Address", type: "text", width: 200 },
            { name: "Country", type: "select", items: countries, valueField: "Id", textField: "Name" },
            { name: "Married", type: "checkbox", title: "Is Married", sorting: false },
            { type: "control" }
        ]
    });
    
});

0 个答案:

没有答案