Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

mysql - Simple Express program for querying a result

I have a snippet of Express code

Below what i am trying to do is pass the table name to keyName by extracting from the request

But I am facing deaslock

i wanted to know whether i am following proper protocols for JSON response

[Part-of-Express-Code]

app.get('/RestaurantDesc/:Key',function(request,response,next){

    var keyName=request.query.Key;
    var name_of_restaurants, RestaurantTimings;
    async.series( [
        // Get the first table contents
        function ( callback ) {
            connection.query('SELECT * FROM ',keyName, function(err, rows, fields)
                {
                        console.log('Connection result error '+err);
                        name_of_restaurants = rows;
                        callback();
                });
        },
        // Get the second table contents
        function ( callback ) {
        connection.query('SELECT * FROM RestaurantTimings', function(err, rows, fields)

            {
                    console.log('Connection result error '+err);
                    RestaurantTimings = rows;
                    callback();
            });
        }

   // Send the response
], function ( error, results ) {
    response.json({
        'restaurants' : name_of_restaurants,
        'RestaurantTimings' : RestaurantTimings
    });
} );
} );

I am getting the output as Cannot GET /RestaurantDesc/

Any Ideas

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

your route should be path, A path that you can access through GET request. for ex: you should be able to access it through

http://example.com/RestaurantDesc/anyKeyHere

and in your code you have

var keyName = request.query.Key

req.query contains query variables see http://expressjs.com/api.html#req.query

So your keyName variable won't contain anyKeyHere.

req.params.Key will contain value anyKeyHere;

but you will need to pass it in url path.

if you need to pass key data in query you can do this.

app.get('/RestaurantDesc',function(request,response,next){
   var keyName=request.query.Key;
 });

and pass key like this in your url

http://example.com/RestaurantDesc/?Key=restaurnetkeyHere

Try going through guide in express site and understand routings and how it works.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...