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
您可能無法正確聲明第二個代碼塊中的db變量。自從您在其後面聲明'db =('./../ db.js')''變量後,您需要在var util = require('util')'之後添加逗號。試一試 –
我試過了,它仍然給我同樣的錯誤 – lagfvu
你確定'db =('./../ db.js');'指向正確的文件嗎?索引函數上的'db.listCustomers'是否工作? –