3

I am running a basic web application which is going to do simple queries against a mongoDb via mongoose in node.js running express. If I do a simple find(), i can return the entire dataset, however, when I try to pass in form data as a variable to find a specific restaurant ie find({restaurantname: Arbys}), I can't get a response. I am using handlebars for passing variables back to the client. It seems as though it is a routing issue but I may me misunderstanding how to handle GET/POST

This is the js which does the querying

//external packages
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var csrf = require('csurf');
var passport = require('passport');

//organic packages & models
var restData = require('../models/restaurants');



/* GET Mongo home page. */
router.get('./mongo', function(req,res,next){
    res.render('mongo');
});

//Get Data  **This works fine**
router.get('/get-data', function(req,res,next){
    restData.find()
        .then(function(doc){
            res.render('mongo/mongo', {items: doc});
        })
});

//Goes to query page with form input 
router.get('/query-data', function(req,res,next){
   res.render('mongo/mongo-query');
});

//Query specific data **This does not work**
router.get('/query-data-details', function(req,res,next){
    var RestaurantName = req.query['RestaurantName'];
    if (RestaurantName.length == 0) {
        res.render('mongo/mongo', {items: "No Docs Found"});
    } else {
        restData.find({name: RestaurantName})
            .then(function (doc) {
                res.render('mongo/mongo', {items: doc});
                console.log(RestaurantName);
            });
    }
});

This is the handlebars script which successfully returns the find() with no query parameters

<div class="container float-left">
        <a href="/mongo/get-data" <button type="button" class="btn btn-primary">Load All Data</button></a>
        <a href="/mongo/query-data" <button type="button" class="btn btn-primary">Query Specific Data</button></a>
        <h2>Data</h2>
        <p>Retrieves restauraunt data by Name, Cuisine, Borough and Address
        <div class="table-responsive">
            <table class="table table-bordered">
                <thead>
                <tr>
                    <th>Name</th>
                    <th>Cuisine</th>
                    <th>Borough</th>
                    <th>Address</th>
                </tr>
                </thead>
                <tbody>
                {{# each items }}
                <tr>
                    <td>{{this.name}}</td>
                    <td>{{this.cuisine}}</td>
                    <td>{{this.borough}}</td>
                    <td>{{this.address}}</td>
                </tr>
                {{/each}}
                </tbody>
            </table>

        </div>
    </div>

</body>

and finally, this is what I have thus far for trying to query a specific restaurant name

<form method="get" action="/query-data-details">
    <input type=text name="RestaurantName">
    <input type="submit">
</form>
</body>

app.js portion

var routeshome = require('./routes/index');
var routesmongo = require('./routes/mongo');
var users = require('./routes/users');

var app = express();

require('./config/passport');

// view engine setup
app.engine('hbs', hbs({extname: 'hbs', defaultLayout: 'layout', layousDir: __dirname + '/views/layouts' }));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(expressValidator());
app.use(cookieParser());
app.use(expressSession({secret: 'b5Frapradra5wen', saveUninitialized: false, resave: false}));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routeshome);
app.use('/users', users);
app.use('/mongo', routesmongo);
3
  • By "can't get a response" do you mean you never get back an HTTP response?
    – rtf
    Jun 15, 2016 at 17:52
  • more specifically, I get a "Not Found" at this URL localhost:3000/query-data-details?RestaurantName=Test
    – Mark
    Jun 15, 2016 at 18:01
  • 1
    Try changing the following: var RestaurantName = req.body.RestaurantName;
    – AndreaM16
    Jun 15, 2016 at 18:08

1 Answer 1

-2

It seems you are sending RestaurantName in url params ...

try changing your route from "/query-data-details" to "/query-data-details/:RestaurantName" then you can get RestaurantName var RestaurantName = req.query['RestaurantName'];

1
  • No luck but I think I may have an issue with my mapping in my app.js file, I a added the relevant parts of the app file up top. I think i need to route through there to my mongo.js file?
    – Mark
    Jun 16, 2016 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.