error in forEach in mongodb

48 views
Skip to first unread message

Eliel Santos

unread,
Feb 15, 2016, 5:50:19 PM2/15/16
to mongodb-user
Hi everyone ,
i have this code when i try to run the erro in mongodb
follows the code :

var insertUsers = (function () {
  var nomes = [
    'jose','carlos','pedro','joao', 'joaquim' ,
    'raul' , 'vinicius' , 'maria' , 'madalena' ,
    'carla'
  ];
  
  var usersNames = [
      'joseMilGrau','carlosFumacinha','pedroPedroca','joaoDosMeuCunhao',
      'joaquimFumaFinin','raulConsiderado','Vivi','mariaMarola','madalenaLena',
      'carlaPedregum'
    ];

  var usuarios = [];
  
  var _insertData = (function () {
    for(var i = 0; i < nomes.length; i++ ) { 
        var user = {
          name:nomes[i],
          bio:"BioExemple",
          date_register:new Date(),
          avatar_path:"particaodeImagens",
          auth: {
          userName:usersNames[i],
          email:usersNames[i]+"@webschool.com.br",
          password:"123",
          last_access:new Date(),
          online:true,
          disabled:false,
          hash_token:"Tokeen teste"
          },
          settings: {
          background_path:"Background qualquer"
          }
        };
      usuarios.push(user);    
    }
    db.usuarios.insert(usuarios);
  });
  
  var _showMessage = function() {
    console.log("Messagem show!");
  };
  
  return {
    insertData : _insertData,
    showMessage : _showMessage
  };
})();

insertUsers.insertData();





Wan Bachtiar

unread,
Feb 16, 2016, 12:44:15 AM2/16/16
to mongodb-user

Hi Eliel,

If I executed the script via mongo shell i.e. mongo yourscript.js , it executes without any error. The script inserts ten documents into usuarios collection, a document for each element in nomes and usersNames zipped.

I also noticed there is no actual forEach() in your script. Perhaps you are missing some part of the script ?

Could you provide the followings:

  • What you are trying to achieve.
  • What errors you are seeing and any log messages.
  • MongoDB version.

Kind regards,

Wan.

Reply all
Reply to author
Forward
0 new messages