Saturday, 6 May 2017

cannot fetch records from mongo collection

i am new to nodejs and my first question on stackoverflow In my application a database named passport has two collection users for storing login information buyers for storing buyers information in my server.js i have following code

var configDB = require('./config/database.js');
app.set('views', __dirname + '/views');
                    app.set('view engine', 'ejs');
                    mongoose.connect(configDB.url); // connect to our database
                    require('./config/passport')(passport);
                     ........
                     ........ 


    **My routes.js content are**



                      module.exports = function(app, passport) {
                      var mongoose = require('mongoose');
                      // normal routes 
                      app.get('/', function(req, res) {
                      res.render('index.ejs');
                      });
                       ........

                     ***inserting records to mongo db***

                       app.post('/addbulletin', function(req, res){
                       function randomNumber(len) {
                        var randomNumber;
                         var n = '';
                         for(var count = 0; count < len; count++) {
                      randomNumber = Math.floor(Math.random() * 10);
                       n += randomNumber.toString();
                     }
                    return n;
                       };

                      var Tr_id = randomNumber(9);


                   var Schema = new mongoose.Schema({
                   Tr_id: String,
                   userID : String ,
                   SellerBank   : String,
                   SellerAccountno: String,
                   SellerName   : String,
                   Amount:Number});
                   var buyer = mongoose.model('buyer', Schema);
                   new buyer({  Tr_id: Tr_id,
                   userID: user._id ,
                   SellerBank    : req.body.SellerBank,
                  SellerAccountno: req.body.SellerAccountno,
                  SellerName  : req.body.SellerName,
                      Amount  : req.body.Amount         
                      }).save(function(err, doc){
                      if(err) res.json(err);
                  else    res.send('Successfully inserted! your Transaction);                         
                  console.log('TransactionID');
                  });
                  });



                       app.get('/viewall',function(req,res)
                            {
                              buyer.find({},function(err,docs){
                               if(err) res.jason(err);
                                else res.render('display',{users:docs});
                                      });

                  db.collection('url').find({}, function(err, products){
                                if(err){
                                 console.log(err);
                              res.json(err);
                                  }
                                      else{
                                res.json(products);
                                       }
                                     });

                       content of display.ejs   
                          ul
                         for each buyer in users
                         li #{buyer.userID}#{buyer.SellerBank} 

> when i execute the script and want to /view all content of collection
> buyer redering display.ejs it gives reference error buyer not defined
> please help....



via vikas singh