我无法在快递中调用我的add函数

时间:2016-02-09 07:40:29

标签: javascript node.js express

 var customerDb = {};
    var id_inc = 0;

    exports.listCustomers = function () {
        return customerDb;
    };

    exports.addCustomer = function (customer) {
        id_inc = id_inc + 1;
        customer.id = id_inc;
        customerDb[customer.id] = customer;
    };


    exports.getCustomerById = function (id) {
        return customerDb[id];
    };

    exports.deleteCustomer = function (id) {
        customerDb[id].remove();
    };

    exports.updateCustomer = function (customer) {
        customerDb[customer.id] = customer;
    }

上面是我用作数据库的js文件,它有一个用于保存数据的数组,还有用于与数据交互的函数。

var util = require('util')
db = ('./../db.js');

exports.index = function (req, res)
{
    res.render('customer/index',
        {
        title:"Customer List",
        customers: db.listCustomers
    })
}

exports.create = function (req, res)
{
    res.render('customer/create')
}

exports.createCustomer = function (req, res)
{

    db.addCustomer(
        {name: req.body.name, email: req.body.email, telephone: req.body.telephone}
    );

    res.redirect('/customer');
}

这是我的路径JS文件,我用它来调用某些功能,具体取决于所请求的页面。调用createCustomer路由时,要运行的addCustomer函数将返回错误“TypeError:db.addCustomer is not a function”。下面是我提交数据到路线的玉石表格。

doctype
html
    head
        title Add new customer
        link(rel='stylesheet', href='/styles/style.css')
    body
        h3 Customer Details
        form(id='form', method='post', action='/customer/create', enctype='multipart/form-data')
            label(for='Name') Name
            input(type='text', id='name', name='name')
            label(for='email') Email
            input(type='email', id='email', name='email')
            label(for='telephone') Telephone
            input(type='tel', id='telephone', name='telephone')
            input(type='submit', value='Add Customer')

此外我的listCustomers函数似乎不起作用,我在我的jade模板中有一个函数来迭代结果并显示它们,“客户中的每个项目”,但它返回错误“无法读取属性”长度'未定义“。

table(class='gridtable')
    tr
        th Id
        th Name
        th Email
        th Telephone
        th Edit
        th Delete
    each item in customers.length ? customers : ['There are no customers']
        tr
            td
                a(href='/customer/details/#{item.id}') #{item.id}
            td #{item.name}
            td #{item.email}
            td #{item.telephone}
            td
                a(href='/customer/edit/#{item.id}') Edit
            td
                a(href='/customer/delete/#{item.id}') Delete

2 个答案:

答案 0 :(得分:2)

您的数据库模块中缺少

select count(pe.id) as register_count, pt.type from pack_registration pr cross join pack_type pt left join pack_event pe on pr.pack_event_id = pe.id and pe.pack_type_id = pt.id where pr.current_plan = 1 and pr.user_id = 1 and pr.created_at between '2016-01-01' and '2016-01-31' group by pt.type

require

加入db = ('./../db.js');

require

现在您的数据库模块可供使用。

至于你的第二个问题(db = require('./../db.js'); 函数似乎不起作用),我不知道你是如何将listCustomers返回给DOM的。需要更多代码。

答案 1 :(得分:0)

为此db创建类,并改为使用回调:

db.js

var Db = {
    id_inc: 0,
    customerDb: {},

    listCustomers: function (callback) {
        callback(this.customerDb);
    },

    addCustomer: function (customer, callback) {
        this.id_inc++;
        customer.id = this.id_inc;
        this.customerDb[customer.id] = customer;
        callback();
    },


    getCustomerById: function (id, callback) {
        callback(this.customerDb[id]);
    },

    deleteCustomer: function (id) {
        this.customerDb[id].remove();
        return;
    },

    updateCustomer: function (customer) {
        this.customerDb[customer.id] = customer;
        return;
    }
};

module.exports = Db;

和您的controller.js

var util = require('util');
var db = require('/path/to/db.js');

exports.index = function (req, res)
{
    db.listCustomers(function(customers) {
      res.render('customer/index',
        {
        title:"Customer List",
        customers: customers
      })
    })
}

exports.create = function (req, res)
{
    res.render('/customer/create')
}

exports.createCustomer = function (req, res)
{
    db.addCustomer({name: req.body.name, email: req.body.email, telephone, req.body.telephone}, function() {
         res.redirect('/customer');
     });
}

还有一件事:您在快递中使用POST请求,因此您需要bodyparser模块。安装它。

我希望这会有所帮助!

干杯,