当路由包含在不同的文件中时未定义猫鼬架构



为了清理我的代码,我决定将所有架构和路由放入目录中的不同文件中,并在我的应用程序中需要它们.js。 每个架构对应于每个路由。 除了一条路线之外,我的所有路线都让它工作,但对于一条特定的路线,我无法找出为什么我把它定为未定义。

这是我在应用程序中的代码.js(主文件(

// Schemas
const Transaction = require ("./models/transaction");
User              = require ("./models/user");
Ticket            = require ("./models/ticket");
Job               = require ("./models/job");
Client            = require ("./models/client");
// Routes
require("./routes/users")(app);
require("./routes/tickets")(app);
require("./routes/clients")(app);
require("./routes/jobs")(app);
require("./routes/transactions")(app);

所有这些都有效,除了我的交易路线。 下面是它的架构:

// =======================Transaction Schema "./models/transaction"
var transactionSchema = new mongoose.Schema({
job: String,
client: String,
deposited_by_user: String,
transaction_info:{
amount: mongoose.Decimal128,
method: String,
receipt_number: String,
date: {type: Date}
},
billing_address: {
street: String,
city: String,
state: String,
zip: String
},
notes: String,
date_added: {type: Date, default: Date.now}
});
module.exports = mongoose.model("Transaction", transactionSchema);  

及其路线:

module.exports = function(app) {
// =======================Transaction  "./routes/transactions"
// index
app.get("/transactions", function(req, res){
Transaction.find({}, function(err, transactions){    // It is at this line where it is telling me that "Transaction" is undefined.  However, with this code pasted into the app.js exactly the same as it is here, it works fine.
if(err){
console.log("error")
} else {                                        
for (let i = 0; i < transactions.length; i++){
transactions[i]["transaction_info"]["new_amount"] = numberWithCommas(transactions[i]["transaction_info"]["amount"]);
}
res.render("transactions", {transactions: transactions});
}
});
});
// new
app.get("/transactions/add", async function(req, res){
let endCollections = [];
for (let i = 0; i < collections.length; i++){
await collections[i].find({}, function(err, foundCollection){ 
if (err) {
console.log(err);
} else {
endCollections[i] = foundCollection;                
}
});
}
res.render("add_transaction", {users: endCollections[0], clients: endCollections[2], jobs: endCollections[3]}); 
});
// show
app.get("/transactions/:id", function(req, res){
Transaction.findById(req.params.id, function(err, foundTransaction){
if (err){
console.log(err);
} else {
//  Redirect
let price = numberWithCommas(foundTransaction["transaction_info"]["amount"]);
res.render("transaction", {transaction: foundTransaction, price: price});
}
});
});
// edit
app.get("/transactions/:id/edit", function(req, res){
Transaction.findById(req.params.id, async function(err, foundTransaction){
if (err){
console.log("error")
} else {
let endCollections = [];
for (let i = 0; i < collections.length; i++){
await collections[i].find({}, function(err, foundCollection){ 
if (err) {
console.log(err);
} else {
endCollections[i] = foundCollection;                
}
});
}       
let deposit_date = foundTransaction["transaction_info"]["date"];
deposit_date = moment(deposit_date).format("MM-DD-YYYY");
res.render("edit_transaction", {transaction: foundTransaction, users: endCollections[0], clients: endCollections[2], jobs: endCollections[3], deposit_date: deposit_date});
}
});
});
// create
app.post("/transactions", function(req, res){ 
//  Create Transaction
Transaction.create(req.body.transaction, function(err, newTransaction){
if (err){
console.log(err)
} else {
//  Redirect
res.redirect("/transactions");
}
});
});
// update
app.put("/transactions/:id", function(req, res){ 
//  Update transaction
Transaction.findByIdAndUpdate(req.params.id, req.body.transaction, function(err, updatedTransaction){
if (err){
console.log(err)
} else {
//  Redirect
res.redirect("/transactions/" + req.params.id);
}
});
});
// delete
app.delete("/transactions/:id", function(req, res){ 
//  Delete job
Job.findByIdAndRemove(req.params.id, function(err, deletedTransaction){
if (err){
console.log(err)
} else {
//  Redirect
res.redirect("/transactions");
}
});
});
}

我不认为架构是问题所在,因为当我将事务路由代码直接粘贴到我的应用程序.js文件中时,逐个字符,它工作正常。 但是,随着它的拆分(并且代码在事务.js文件中保持不变(,当我在以 Transaction.find 开头的代码部分加载"事务未定义"页面时,我收到错误

总的来说,我不明白为什么当路由在app.js文件中时,它工作得很好,但是当它在单独的文件中时,Transaction变量被认为是未定义的;尽管它的建模与我的其他路由和模式完全相同。 这里有我没有看到的东西吗? 谢谢。

1( 而不是:

app.get("/transactions", function(req, res){
.
.
app.get("/transactions/:id", function(req, res){

也许只是:

app.get("/", function(req, res){
.
.
app.get("/:id", function(req, res){

等等?

2(

Transaction.find({}, function(err, transactions){ 

而不是 {} 尝试 ((

Transaction.find((), function(err, transactions){ 

看起来您正在传递一个空对象

相关内容

  • 没有找到相关文章

最新更新