我正在尝试创建一个简单的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);
});


现在,当我在网络浏览器中键入呼叫http://10.205.116.141:3030/customer/1013686356时,我得到了以下信息:

Error: Expected 4 or 0 byte int (10)


控制台日志显示如下:

Select * from entcustinfo where ent_cust_id = ?


我以为?是传递网址终点的方法?

编辑:

我认为这与execute vs executeAsPrepared有关...我使用executeAsPrepare,出现此错误:
TypeError: undefined is not a function

最佳答案

所以这就是我的工作方式:

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


我将[req.params.ent_cust_id]移至var query

关于javascript - nodejs传入URL的终点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32210799/

10-11 14:16