当路由包含在其他文件中时,未定义Mongoose模式

为了清理代码,我决定将所有模式和路由放入目录中的不同文件中,并在app.js中要求它们。每个模式对应于每个路由。除了我的一条路线以外,所有其他路线都可以使用,但是对于特定的一条路线,我无法找出为什么我将其定义为未定义。

这是我的app.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");
        }
    });
});

}

我不认为Schema是问题所在,因为当我将Transaction路由代码直接粘贴到我的app.js文件中时,它可以正常工作。但是,将其拆分(代码在transactions.js文件中保持完全相同)时,在以Transaction.find开头的代码部分加载“ Transaction is undefined”页面时,出现了错误

总的来说,我不明白为什么当路由位于app.js文件中时,它可以正常工作,但是当路由位于单独的文件中时,Transaction变量被视为未定义;尽管建模与我的其他路由和架构完全相同,但这一切都是如此。这里有什么我没看见的吗?谢谢。

评论