sqlanywhere / node-sqlanywhere

SAP SQL Anywhere Database Client for Node
Apache License 2.0
39 stars 37 forks source link

Error: Code: -2001 Msg: Invalid Object #29

Closed miljosh68 closed 6 years ago

miljosh68 commented 6 years ago

Node: v10.7.0 Express: v4.16.3 sqlanywhere: v1.0.23

Trying my hands on Node, Express and SQL AnyWhere. The database server is running SQL AnyWhere version 12 in network mode running on a PC named 'server2' listening on port '2639' (not the default 2638 port).

The 'index.html' submits form using ajax to '/showrptlist' route which uses the project ID in the SQL and tries to retrieve data from the database. But, in the console, I get "Error: Code: -2001 Msg: Invalid Object" error. Earlier I was able to display the result in the web page but now, I get the above error all the time.

How to fix it and display the data in the web page?

Here is code from my index.js.

const express = require('express');
const app = express();
const path = require('path');

app.set('view engine', 'pug');
app.set("views", path.join(__dirname, "./views"));
app.use(express.static(path.join(__dirname, './public')));

const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

const sqla = require('sqlanywhere');
const conn = sqla.createConnection();
conn_params = {
    Host: 'server2:2639',
    UserId: '*****',
    Password: '*****', 
    DatabaseName: '*****'
};

conn.connect(conn_params, function(err) {
    if (err) {
        console.log('A Database Error has occured while connecting.')
    } else {
        console.log('Database connection successful!');
    }
});

app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname + '/index.html'));
});
app.post('/showrptlist', function(req, res) {
    conn.exec("SELECT wr.col1 + ' - ' + wr.col2 + ' - ' +  wr.col3 + ' - ' + wr.col4  'test_number', wr.col1, wr.col2, wr.col3, wr.col4, wr.web_report_name, wr.rpt_descr, wr.prjnum, prfx.prfx_desc FROM mtl_webreports wr left outer join mtl_test_prfx prfx on wr.test_id = prfx.prfx WHERE wr.prjnum =  ?  ORDER BY wr.col4 desc, wr.col1 asc, wr.col2 asc, wr.col3 asc", [req.body.txtProj], function (err, rows) {
        if (err) {
            console.log('Could not search verified reports. ' + err);
        } else {
            res.send(rows);
        }
    });
});

app.use(function (req, res, next) {
    res.status(404).send("Sorry, can't find that!")
})

app.listen(3000, () => console.log('Listening on port 3000'));

if (conn.connected()) {
    conn.disconnect();
    console.log('Disconnected from Database.');
}
gperrow-SAP commented 6 years ago

I tried to reproduce this but was unable to make it fail. I had to guess at your database schema and added a row of test data but everything seemed to work as expected. Can you send me the schema for the two tables involved?

miljosh68 commented 6 years ago

Thanks @gperrow-SAP. I am not sure under what circumstances it showed me the error. But, I was able to connect and was able to view the intended report list. But, for some reason, the 'conn.connected()' always returns 'false'. Do you know why? The console never shows the 'Disconnected from Database.' message.

gperrow-SAP commented 6 years ago

That's the nature of asynchronous programming. Your program has called conn.connect() which is executed asynchronously (sort of on another thread). Similarly, app.listen() is executed asynchronously. When your main thread gets to the if( conn.connected() ) line, the connect() function hasn't run yet, so conn.connected() is false. You can do something like this to catch CTRL-C:

process.on( 'SIGINT', function() {
    if (conn.connected()) {
        conn.disconnect();
        console.log('Disconnected from Database.');
    }
    process.exit( 1 );
});

That supplies node with a function to call when the SIGINT signal is received, i.e. when the user hits CTRL-C.