node.js - CANNOT GET with mongodb and nodejs -


hello i'm trying book name db when execute script localhost shuts down , have restart. error :

referenceerror: text not defined

here server.js

    var express     = require('express');     mongoclient = require('mongodb').mongoclient,     app = express(),     mongourl = 'mongodb://localhost:27017/firstapp'; var bodyparser  = require('body-parser'); var morgan      = require('morgan'); var mongoose    = require('mongoose'); var passport    = require('passport'); var redisclient = require('redis').createclient; var redis       = redisclient(6379, 'localhost'); var config      = require('./config/database'); // db config file var user        = require('./app/models/user'); // mongoose model var products    = require('./app/models/products'); //get mongoose model var makeissue   = require('./app/models/makeissue'); //get mongoose model var port        = process.env.port || 8080; var jwt         = require('jwt-simple'); var access      = require('./config/database.js');  mongoclient.connect(mongourl, function(err, db) {     if (err) throw 'error connecting database - ' + err;         // our request parameters         app.use(bodyparser.urlencoded({ extended: false }));         app.use(bodyparser.json());          // log console         app.use(morgan('dev'));          // use passport package in our application         app.use(passport.initialize());          // demo route (get http://localhost:8080)         app.get('/', function(req, res) {               res.send('the api @ http://localhost:' + port + '/api');         });          // connect database         mongoose.connect(config.database);          // pass passport configuration         require('./config/passport')(passport);          // bundle our routes var apiroutes = express.router();   // create new user account (post http://localhost:8080/api/signup) apiroutes.post('/signup', function(req, res) {   if (!req.body.name || !req.body.password || !req.body.email) {     res.json({success: false, msg: 'please pass name , password , email.'});   } else {     var newuser = new user({       name: req.body.name,       password: req.body.password,       email: req.body.email     });     // save user     newuser.save(function(err) {       if (err) {         return res.json({success: false, msg: 'username exists.'});       }       res.json({success: true, msg: 'successful created new user.'});     });   } });  // route authenticate user (post http://localhost:8080/api/authenticate) apiroutes.post('/authenticate', function(req, res) {   user.findone({     name: req.body.name   }, function(err, user) {     if (err) throw err;      if (!user) {       res.send({success: false, msg: 'authentication failed. user not found.'});     } else {       // check if password matches       user.comparepassword(req.body.password, function (err, ismatch) {         if (ismatch && !err) {           // if user found , password right create token           var token = jwt.encode(user, config.secret);           // return information including token json           res.json({success: true, token: 'jwt ' + token});         } else {           res.send({success: false, msg: 'authentication failed. wrong password.'});         }       });     }   }); });  apiroutes.post('/book', function (req, res) {         if (!req.body.title || !req.body.author) res.status(400).send("please send title , author book");         else if (!req.body.text) res.status(400).send("please send text book");         else {             access.savebook(db, req.body.title, req.body.author, req.body.text, function (err) {                 if (err) res.status(500).send("server error");                 else res.status(201).send("saved");             });         }     });  apiroutes.get('/book/:title', function (req, res) {         if (!req.param('title')) res.status(400).send("please send proper title");         else {             access.findbookbytitle(db, req.param('title'), function (book) {                 if (!text) res.status(500).send("server error");                 else res.status(200).send(book);             });         }     });    // create new product (post http://localhost:8080/api/productsignup) apiroutes.post('/resources/productsignup', function(req, res) {   if (!req.body.name || !req.body.serialnumber) {     res.json({success: false, msg: 'please pass name , serial number.'});     } else {     var newproducts = new products({       name: req.body.name,       serialnumber: req.body.serialnumber          });     // save product     newproducts.save(function(err) {       if (err) {         return res.json({success: false, msg: 'product exists.'});       }       res.json({success: true, msg: 'successful created new product.'});     });   } });  apiroutes.post('/resources/createpost', function(req, res) {   if (!req.body.issue) {     res.json({success: false, msg: 'please pass issue.'});     } else {     var newmakeissue = new makeissue({       issue: req.body.issue         });     // save product     newmakeissue.save(function(err) {       if (err) {         return res.json({success: false, msg: 'post exists.'});       }       res.json({success: true, msg: 'successful created new post.'});     });   } });    //display specific product stored in database apiroutes.get('/resources/productinfo/:name' , function(req, res) {   if (!req.param('name')) res.status(400).send("please send proper name");   else{       products.find(products, req.param('name'), function(products) {         if (!text) res.status(500).send("server error");       });     }   });  apiroutes.get('/productinfo' , function(req, res, next) {     products.find( function (err, result) {     if (err) return console.error(err);       res.json(result);   }); });      // route restricted info (get http://localhost:8080/api/memberinfo) apiroutes.get('/memberinfo', passport.authenticate('jwt', { session: false}), function(req, res) {   var token = gettoken(req.headers);   if (token) {     var decoded = jwt.decode(token, config.secret);     user.findone({       name: decoded.name     }, function(err, user) {         if (err) throw err;          if (!user) {           return res.status(403).send({success: false, msg: 'authentication failed. user not found.'});         } else {           res.json({success: true, msg: 'welcome in member area ' + user.name + '!'});         }     });   } else {     return res.status(403).send({success: false, msg: 'no token provided.'});   } });  gettoken = function (headers) {   if (headers && headers.authorization) {     var parted = headers.authorization.split(' ');     if (parted.length === 2) {       return parted[1];     } else {       return null;     }   } else {     return null;   } };   // connect api routes under /api/* app.use('/api', apiroutes); module.exports = apiroutes;         app.listen(8080, function() {         console.log('listening on port 8080');     }); }); 

and database.js

    module.exports = {   'secret': 'di.ionio.gr',   'database': 'mongodb://localhost/firstapp' };      module.exports.savebook = function (db, title, author, text, callback) {         db.collection('text').save({             title: title,             author: author,             text: text         }, callback);     };      module.exports.findbookbytitle = function (db, title, callback) {         db.collection('text').findone({             title: title         }, function (err, doc) {             if (err || !doc) callback(null);             else callback(doc.text);         });     }; 

what doing wrong?

not sure if there other issues code. error referenceerror: text not defined, might compiler complaining cannot determine text declared in following lines?

apiroutes.get('/book/:title', function (req, res) { ...   if (!text) res.status(500).send("server error"); ...  apiroutes.get('/resources/productinfo/:name' , function(req, res) { ...   if (!text) res.status(500).send("server error"); ... 

if error, might need replace text req.body.text


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -