Home > front end >  MongoDB showing the schema as undefined
MongoDB showing the schema as undefined

Time:06-09

While retrieving documents from database values are shown as undefined in console log. Piece of code that causing that issue.

const stockSchema = mongoose.Schema({
  rollingStockNumber: String,
  docs: {
    naturka: String,
    address: String,
    coefficient: String,
    wheel: String,
    weight: Number
  }
});
const Stock = mongoose.model('Stock', stockSchema);
for(i=0; i<naturkaMinifiedArrayReady.length; i  ){
    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) {
        console.log(err)
      }
    });
};

app.post('/indexReplaced.html', (req, res) => {
  Stock.find({}, function(err, foundStocks){
    if (!err) {
      res.render('indexCalculated', {
        stock: foundStocks.rollingStockNumber
      });
      console.log(foundStocks.rollingStockNumber)
    } else {
      console.log(err)
    }
  })
});

*There was no problem with inserting arrays, also shown correctly in database:

{ "_id" : ObjectId("629f6cfea42a2524fbc810c6"), "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("629f6cfea42a2524fbc810c7"), "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("629f6cfea42a2524fbc810c8"), "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("629f6cfea42a2524fbc810c9"), "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("629f6cfea42a2524fbc810ca"), "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("629f6cfea42a2524fbc810cb"), "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 }
{ "_id" : ObjectId("629f6cfea42a2524fbc810cc"), "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("629f6cfea42a2524fbc810cd"), "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("629f6cfea42a2524fbc810ce"), "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 }

However, while logging into console showing undefined. Thanks in advance.

CodePudding user response:

Stock.find() will return an array but you're treating foundStocks as a single document.

If you want to log all the numbers:

for (const stock of foundStocks) {
  console.log(stock.rollingStockNumber);
}
  • Related