传递表单数据不会给出响应

时间:2016-06-15 17:42:29

标签: javascript node.js mongodb mongoose handlebars.js

我正在运行一个基本的Web应用程序,它将通过运行express的node.js中的mongoose对mongoDb进行简单查询。如果我做一个简单的find(),我可以返回整个数据集,但是,当我尝试将表单数据作为变量传递给一个特定的餐馆,即查找({restaurantname:Arbys})时,我无法得到一个响应。我正在使用把手将变量传递回客户端。好像这是一个路由问题,但我可能会误解如何处理GET / POST

这是执行查询的js

//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);
            });
    }
});

这是成功返回没有查询参数的find()的把手脚本

<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>

最后,这是我到目前为止尝试查询特定餐馆名称

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

app.js部分

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);

1 个答案:

答案 0 :(得分:-2)

好像你在url params发送了RestaurantName ......

尝试更改您的路线 “/ q​​uery-data-details”来 “/查询数据的详细信息/:RestaurantName” 那么你可以得到RestaurantName var RestaurantName = req.query ['RestaurantName'];