Home > OS >  Mongoose Collections List Method
Mongoose Collections List Method

Time:07-25

Im new about the Mongoose. So I'm trying to do collections table.

I've this type of Schema for operators:

const operatorSchema = new Schema({
    operatorName: {
        type: String
    },
    productsSerialNumber:[{productSerialNumbers: String}],
    operatorSerialNumber: {
        type: Number
    },
    users:[{
        email:String,
        payment:Number,
        paymentsData: Date,
      }],
    operatorState:{
        type: Boolean,
    }

});

I want to create table like this, users using some products and I want to list which user using this product also where and when.

As below lines:

OperatorName|productsSerialNumber|username|mail|Date|Payment

But in nodejs side, I couldnt success because users is an Array and how can I get every line from database.

I'm trying like this:

const operatorArray = []
const userArray = []

exports.getOperatorInformations = (req, res, next)=>{
 
    

    Operators.find({},function(err,docs){
        docs.forEach(function(dataCatch){
            console.log(dataCatch)
            operatorArray.push(dataCatch)
            
            res.render('home',{operators: operatorArray });

        })
     })    
    }
}

operatorArray includes all informations, so I send to ejs side:

       <% operators.forEach(function(data,index){ %>
                                    <%console.log(index)%>
                                    <tr>
                                    <td>
                                        <%=  data.operatorName %>
                                     </td>
                                     <td>
                                        <%=  data.productsSerialNumber %>
                                     </td>
                                     <td>
                                        <%=  data.productsSerialNumber %>
                                     </td>
                                    <td>
                                       <%=  data.users[index].email %>
                                    </td>
                                    <td>
                                        <%=  data.users[index].paymentsData %>
                                     </td>
                                    <td>
                                        <!-- <%=  data.users[index].payment %> -->
                                     </td>
                                     </tr>
                                     <% })%>

index return length of the operators object. But I want to see lenght of user, when I try with operators.users.forEach It doesnt work. How can I do this ?

CodePudding user response:

You could return the docs array directly to the ejs:

exports.getOperatorInformations = (req, res, next) => {
  Operators.find({}, function (err, docs) {
    if (err) console.log(err)
    else res.render('home', { operators: docs });
  });
};

And change your ejs to:

<% operators.forEach(function(data){ %> 
    <% data.users.forEach(function(user){ %> 
    <tr>
      <td><%= data.operatorName %></td>
      <td><%= data.productsSerialNumber %></td>
      <td><%= data.productsSerialNumber %></td>
      <td><%= user.email %></td>
      <td><%= user.paymentsData %></td>
      <td><!-- <%=  user.payment %> --></td>
    </tr>
    <% })%>
<% })%>
  • Related