2016-02-09 72 views
-1
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不是函數」。下面是我提交數據到路由的玉表格。

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功能似乎並沒有工作,我有一個功能,每一個在我玉模板,通過結果來遍歷並顯示出來,「在每一個客戶項目」,但它返回的「錯誤無法讀取未定義的屬性「長度」。

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 
+0

您可能無法正確聲明第二個代碼塊中的db變量。自從您在其後面聲明'db =('./../ db.js')''變量後,您需要在var util = require('util')'之後添加逗號。試一試 –

+0

我試過了,它仍然給我同樣的錯誤 – lagfvu

+0

你確定'db =('./../ db.js');'指向正確的文件嗎?索引函數上的'db.listCustomers'是否工作? –

回答

2

require從數據庫模塊缺失:

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

附上require

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

現在你的數據庫模塊可供使用。

至於你的第二個問題(listCustomers函數似乎不工作),我不知道你如何將customerDb返回到DOM。需要更多的代碼。

0

爲你的數據庫是這樣創建類,並使用回調來代替:

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模塊。安裝它也是如此。

我希望這會有所幫助!

乾杯,