Thanks Mike, appreciate the help :)
On Saturday, March 23, 2013 3:47:31 PM UTC, Mike Pilsbury wrote:
>
> Like most io in node, tedious responds asynchronously. However you are
> treating your connect_db function as if it operated synchronously. The
> result array will still be empty when the function returns.
>
> Pushing stuff in to the result array in the 'row' event is fine; that will
> allow you to accumulate the data.
>
> You possibly only want to do
> res.write(results);
> res.end("hello world");
> in the Request's callback function (which is called once the request is
> complete).
>
> So something along these lines.
> if (err) {
> ...
> } else {
> res.write(results);
> res.end("hello world");
> }
> Of course you'll need to pass res to your connect_db function.
>
> Or you could write each result in the 'row' event handler, then
> res.end("hello world");
> in the Request's completion function.
>
>
> On Friday, 22 March 2013 17:06:23 UTC, Ibrahim Gaal wrote:
>>
>> Hi Guys,
>>
>> I'm also very new to node, and would appreciate any help you can provide.
>> I have a http webserver created in node js. once you hit the server I'd
>> like it respond with some json data.
>> I'm able to connect to my sql server database using the tedious module. I
>> can extract data from my tables and write to console, however I'm having
>> problems returning the data back as the http web server's response.
>>
>> please see code below
>>
>> var http = require('http');
>> var s = http.createServer(function (req, res) {
>> res.writeHead(200, {'Content-Type': 'text/plain'});
>>
>> var results = connect_db();
>> res.write(results);
>> res.end("hello world");
>>
>>
>> });
>> s.listen(9090);
>>
>>
>> function connect_db(){
>>
>> // Global variables
>> var Connection = require('tedious').Connection;
>> var Request = require('tedious').Request;
>> var TYPES = require('tedious').TYPES;
>>
>> var result = [];
>> var config = {
>> userName: 'user1',
>> password: 'password',
>> server: 'localhost',
>> "options": {
>> "database": "testdb",
>> }
>> };
>>
>> // create connection instance/object
>> var connection = new Connection(config);
>>
>> connection.on('connect', function(err){
>>
>> if (err){
>> console.log(err);
>> console.log('Unable to connect to database');
>> }
>> else {
>> console.log('connection to database established');
>> }
>>
>> // create Request instance
>> request = new Request("select top 1 id from table1", function(err,
>> rowCount) {
>> // error check request
>> if (err) {
>> console.log(err);
>> console.log('request error');
>> }
>>
>> // close connection after rows return for request
>> if (rowCount)
>> {
>> console.log('finished');
>> connection.close();
>> console.log('db connection closed');
>> }
>>
>> });
>>
>>
>> request.on('row', function(columns) {
>> columns.forEach(function(column) {
>> //console.log(column.value);
>> results.push(column.value);
>> //console.log(foo);
>>
>> });
>> });
>>
>> connection.execSql(request);
>>
>> });
>>
>>
>> return result;
>> }
>>
>>
>>
>> On Wednesday, January 9, 2013 7:37:13 AM UTC, Bhoomi Kakaiya wrote:
>>>
>>>
>>> Hi all,
>>>
>>> I am pretty new to Node. I am using Tediuos to get data from SQL
>>> database.
>>>
>>> Here is the code snippet :
>>>
>>> app.get('/getitems', function(req, res){
>>> res.contentType('application/json');
>>> var config = { userName: 'sa', password: 'pass', server:
>>> '127.0.0.1', options : { database:'Auto'}
>>> };
>>> var connection = new Connection(config);
>>> connection.on('connect', function(err) {
>>> executeStatement(res);
>>> });
>>> function executeStatement(res1) {
>>> console.log(res1.contentType);
>>> request = new Request("select top 3 sItemId [Id], sName [Item], nrate1
>>> [Rate] from bmitem", function(err, rowCount) {
>>> if (err) {
>>> console.log(err);
>>> } else {
>>> console.log(rowCount + ' rows');
>>> }
>>> connection.close();
>>> });
>>>
>>> request.on('row', function(columns) {
>>> columns.forEach(function(column) {
>>> if (column.value === null) {
>>> console.log('NULL');
>>> } else {
>>> console.log(column.value);
>>> }
>>> });
>>> });
>>> request.on('done', function(rowCount, more) {
>>> console.log(rowCount + ' rows returned');
>>> debugger;
>>> res1.send("3");// Here is the problem. Debugger shows res1 is <error>.
>>> });
>>> connection.execSql(request);
>>> }
>>> });
>>>
>>>
>>> The problem is response object has error.
>>> I want to create json of the items returned from db and want to pass it
>>> as json in the ajax request.
>>>
>>> If I return response in request.on("row") it works. Do not understand
>>> why response object is not available on "done" event.
>>>
>>> Thanks in advance.
>>>
>>> Bhoomi.
>>>
>>>
>>>
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.