Rendering database documents inside the templete, EJS

Issue

I cannot render the database documents inside the EJS. It throwing error like "TypeError: req.next is not a function". Piece of code that causing the issue is shown below:

const stockSchema = mongoose.Schema({
  rollingStockNumber: String,
  docs: {
    naturka: String,
    address: String,
    coefficient: String,
    wheel: String,
    weight: Number
  }
});

const Stock = mongoose.model('Stock', stockSchema);
    const stock = new Stock({
      rollingStockNumber: rollingStock,
      docs: {
        naturka: naturkaMinifiedArrayReady[i],
        address: arrayOfWayPlanForm[i],
        coefficient: valueOfCarLength[i],
        wheel: numberOfWheels[i],
        weight: valueOfCarWeight[i]
      }
    });
    stock.save((err) => {
      if (err) {
        res.redirect('/')
      }
    });
};
app.post('/indexReplaced.html', (req, res) => {
  Stock.find({}, function(err, foundStocks){
    if (!err) {
      for (const stock of foundStocks) {
        res.render('indexCalculated', {items: stock.docs.address})
      }
    } else {
      console.log('E', err)
    }
    res.end();
  })
});

Documents inside the MongoDB, there is no problem with inserting them:

{ "_id" : ObjectId("629f6dcda42a2524fbc810db"), "rollingStockNumber" : "922698012567076507200129101700057022060000000", "docs" : { "naturka" : "01 94296670 0271 022 72240  00300 0012 0 0 0 0 01/00 96 04 105 0204 OXP", "address" : "Toshkent", "coefficient" : "1.05", "wheel" : "4", "weight" : 42.4 }, "__v" : 0 }
{ "_id" : ObjectId("629f6dcda42a2524fbc810dc"), "rollingStockNumber" : "922698012567076507200129101700057022060000000", "docs" : { "naturka" : "02 94865987 0271 021 72000  00300 0012 0 0 0 0 01/00 96 04 142 0200 OXP", "address" : "Chuqursoy", "coefficient" : "1.42", "wheel" : "4", "weight" : 41 }, "__v" : 0 }
{ "_id" : ObjectId("629f6dcda42a2524fbc810dd"), "rollingStockNumber" : "922698012567076507200129101700057022060000000", "docs" : { "naturka" : "03 94348224 0271 029 72000  00300 0012 0 0 0 0 01/00 96 04 105 0200 OXP", "address" : "Chuqursoy", "coefficient" : "1.05", "wheel" : "4", "weight" : 49 }, "__v" : 0 }
{ "_id" : ObjectId("629f6dcda42a2524fbc810de"), "rollingStockNumber" : "922698012567076507200129101700057022060000000", "docs" : { "naturka" : "04 94215209 0271 025 72000  00300 0012 0 0 0 0 01/00 96 04 105 0200 OXP", "address" : "Chuqursoy", "coefficient" : "1.05", "wheel" : "4", "weight" : 45 }, "__v" : 0 }
{ "_id" : ObjectId("629f6dcda42a2524fbc810df"), "rollingStockNumber" : "922698012567076507200129101700057022060000000", "docs" : { "naturka" : "05 94336229 0271 024 72000  00300 0012 0 0 0 0 01/00 96 04 105 0200 OXP", "address" : "Chuqursoy", "coefficient" : "1.05", "wheel" : "4", "weight" : 44 }, "__v" : 0 }
{ "_id" : ObjectId("629f6dcda42a2524fbc810e3"), "rollingStockNumber" : "922698012567076507200129101700057022060000000", "docs" : { "naturka" : "09 94284361 0271 029 72240  00300 0012 0 0 0 0 01/00 96 04 105 0208 OXP", "address" : "Toshkent", "coefficient" : "1.05", "wheel" : "4", "weight" : 49.8 }, "__v" : 0 }
{ "_id" : ObjectId("629f6dcda42a2524fbc810e1"), "rollingStockNumber" : "922698012567076507200129101700057022060000000", "docs" : { "naturka" : "07 94866076 0271 023 72000  00300 0012 0 0 0 0 01/00 96 04 142 0200 OXP", "address" : "Chuqursoy", "coefficient" : "1.42", "wheel" : "4", "weight" : 43 }, "__v" : 0 }
{ "_id" : ObjectId("629f6dcda42a2524fbc810e2"), "rollingStockNumber" : "922698012567076507200129101700057022060000000", "docs" : { "naturka" : "08 94331444 0271 030 72240  00300 0012 0 0 0 0 01/00 96 04 105 0200 OXP", "address" : "Toshkent", "coefficient" : "1.05", "wheel" : "4", "weight" : 50 }, "__v" : 0 }
{ "_id" : ObjectId("629f6dcda42a2524fbc810e0"), "rollingStockNumber" : "922698012567076507200129101700057022060000000", "docs" : { "naturka" : "06 94324381 0271 026 72000  00300 0012 0 0 0 0 01/00 96 04 105 0200 OXP", "address" : "Chuqursoy", "coefficient" : "1.05", "wheel" : "4", "weight" : 46 }, "__v" : 0 }

Thanks in advance!

Solution

Well you are trying to render so many times. Try just rendering one time with the sending addresses objects.

app.post('/indexReplaced.html', (req, res) => {
  Stock.find({}, function(err, foundStocks){
    if (!err) {
      const addresses = foundStocks.map((stock) => stock.docs.address);
      res.render('indexCalculated', {items : addresses}); 
    } else {
      console.log('E', err)
    }
    res.end();
  })
});

Answered By – Zahid Çakıcı

Answer Checked By – Marilyn (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.