简体   繁体   English

nodejs传入URL的终点

[英]nodejs passing in the end point of the url

I'm trying to create a simple js app to query a cassandra db like this: 我正在尝试创建一个简单的js应用程序来查询cassandra数据库,如下所示:

var express = require('express');
var cassandra = require('cassandra-driver')
var app = express();
var client = new cassandra.Client({contactPoints: ['127.0.0.1'], keyspace: 'custprd'});

app.get('/', function (req, res) {
  res.send('Hello Ronak!');
});

app.get('/customer/:ent_cust_id', function (req, res, next) {
        var query = 'Select * from entcustinfo where ent_cust_id = ?';
        consolelog('Select * from entcustinfo where ent_cust_id = ?');
        client.execute(query, [req.params.ent_cust_id], function (err, result) {
    if (err) return next (err);
    var row = result.rows[0];
    //Response
    res.json({ent_cust_id: req.params.ent_cust_id, name: row.get('offers')});
});
});


var server = app.listen(3030, function () {
  var host = server.address().address;
  var port = server.address().port;

  console.log('Example app listening at http://%s:%s', host, port);
});

now, when I go an type the call http://10.205.116.141:3030/customer/1013686356 in the web browser, I get this back: 现在,当我在网络浏览器中键入呼叫http://10.205.116.141:3030/customer/1013686356时,我会得到以下信息:

Error: Expected 4 or 0 byte int (10)

and the console log is showing this: 控制台日志显示如下:

Select * from entcustinfo where ent_cust_id = ?

I thought the ? 我以为? was the way to pass in the end point of the url? URL终点的传递方式是什么?

EDIT: 编辑:

I think this has to do with execute vs executeAsPrepared...I use executeAsPrepare, I get this error: TypeError: undefined is not a function 我认为这与execute vs executeAsPrepared有关...我使用executeAsPrepare,出现此错误: TypeError: undefined is not a function

so here is how I got it to work: 所以这就是我的工作方式:

app.get('/customer/:ent_cust_id', function (req, res, next) {
        var query = 'Select * from entcustinfo where ent_cust_id = ?' [req.params.ent_cust_id];
        consolelog('Select * from entcustinfo where ent_cust_id = ?');
        client.execute(query, function (err, result) {
    if (err) return next (err);
    var row = result.rows[0];
    //Response
    res.json({ent_cust_id: req.params.ent_cust_id, name: row.get('offers')});
});
});

I moved [req.params.ent_cust_id] up to var query 我将[req.params.ent_cust_id]移至var query

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM