简体   繁体   中英

Pass large amount of data through res.render (Node)

Trying to pass an object with about as many as 3000 names through the.render function.

Checked everything and came down to conclusion it's not the database, nor the page and it's the data 'copying', I assume, over to the response. I saw someone suggested something about AJAX and changing the response, 10 names at a time, but I'm afraid I am not entirely sure how to execute it.. Code is not on my machine so will try my best to depict it:

in route.js

import addRequestPageController from "../controllers/addRequestPageController"

app.get('/add_request', ... , addRequestPageController.addRequest)

in addRequestPageController.js

import DBConnection from "../configs/connectDB" // This is where the mysql DB connection is configured

let addRequest = function(req, res) {
var obj = {}
try {

 DBConnection.query(..(a query brings 50~ names).., function(err, rows) {
     if (err) {
        res.render("error.ejs")
     } else { // No errors
         obj.docNames = rows
         // Each doctor has firstName and lastName attributes
         res.render("addRequest.ejs", obj)
     }
 })

 DBConnection.query(..(query-3000 rows with drug names, ratings).., function(err, rows) {
     if (err) {
        res.render("error.ejs")
     } else { // No errors
         obj.drugNames = rows
         // Each drug has it's name(varchar) and rating (float value)
         res.render("addRequest.ejs", obj)
     }
 })

} catch{...}

}

module.exports = {
addRequest: addRequest
}

adding the addRequest.ejs for further reference-

in addRequest.ejs

...


<select name="docNames" id="docNames">
      <option value="">--Please choose an option--</option>
      <% docNames.forEach(function(doc) { %>
      <!-- Start of forEach -->
      <option value="<% doc.firstName + " " + doc.lastName %>">
      <!-- that was to put a value of the doctor's name -->
      <% doc.firstName + " " + doc.lastName %>
      <!-- and that was to put the actual value the user sees -->
      </option>
      <!-- End of forEach -->
      <% }) %> 
</select>

<select name="drugNames" id="drugNames">
     <option value="">--Please choose an option--</option>
     <% drugNames.forEach(function(drug) { %> 
     <!-- Start of forEach -->
     <option value="<% drug.drug_name %>">
     <!-- that was to put a value of the drug's name -->
     <% drug.drug_name + " and rating of : " + drug.rating %>
     <!-- that was to put the actual value the user sees -->
     </option>
     <!-- End of forEach -->
     <% }) %> 
</select>

...

So basically I am passing parameters to the page, but it stucks at loading and giving me no errors. Is there a way to change only addRequest to get the desired outcome?

edit: added addRequest.ejs, and edited the addRequestPageController.js. I did however tried to render a different page with nothing in it, and it still failed to load.. feelsbadman

You are calling res.render() twice, the first time without the drugNames data and the second time without the docNames data. Both will likely fail and you can only call res.render() once per request as you only get to send one response per incoming request, not two.

You need to collect all the data for the template first so you have it call before you call res.render() once passing it all the data it needs. Here's one way to do that:

import DBConnection from "../configs/connectDB" // This is where the mysql DB connection is configured

const addRequest = function(req, res) {
    DBConnection.query(..(a query brings 50~names).., function(err, rows) {
        if (err) {
            console.log(err);
            res.render("error.ejs");
            return;
        } else { // No errors
            const obj = { docNames: rows };
            // Each doctor has firstName and lastName attributes
            DBConnection.query(..(query - 3000 rows with drug names, ratings).., function(err, rows) {
                if (err) {
                    console.log(err);
                    res.render("error.ejs");
                    return;
                } else { // No errors
                    obj.drugNames = rows
                    // Each drug has it's name(varchar) and rating (float value)
                    res.render("addRequest.ejs", obj)
                }
            });
        }
    });
}

module.exports = {
    addRequest
};

Also, I'm quite confused why you're using import to load the database module, but then using module.exports . Those two do not go together. Pick one module module or the other, not a mix of the two. import would go with export and require() would go with module.exports . This needs to also be fixed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM