I have my nodejs lambda function connecting to rds and select data and gives me output in log output. Please help me how to pass parms and get output. My code is attached. My nodejs function connecting to rds but I cannot see mydata in execution result.
var mysql = require('mysql');
exports.handler = (event, context) => {
var con = mysql.createConnection({
host: "testdb.ckwywu.ap-south-1.rds.amazonaws.com",
user: "root",
password: "mypassword",
database: "database",
port: "3306",
// debug: true
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
// var sql = "INSERT INTO users (id, name) VALUES (4, 'dfdd')";
var sql = "select * from database.users";
con.query(sql, function (err, result) {
if (err) throw err;
// console.log("1 record inserted");
console.log(result);
context.succeed("done");
});
});
//callback("sucess");
}
Related
The following code connected to Redshift database but returning an empty response even the response will be displayed in the console.log, why?
index.js
const config = {
user: 'user',
database: 'database',
password: 'password',
port: port,
host: 'hostname',
};
var response = [];
console.log('Before Connection');
exports.handler = function index(event, context, callback) {
var redshiftClient = new Redshift(config, {rawConnection:true});
redshiftClient.connect(function(err){
console.log('After Connection');
if(err) throw err;
else{
redshiftClient.query('SELECT * FROM customer', {raw: true}, function(err, data){
if(err) throw err;
else{
response = data;
console.log(data);
redshiftClient.close();
return response;
}
});
}
});
return response;
};
I have used the promise concept and it does returning the response properly for me.
Sample JSON {"user_id": "1001"}
var Redshift = require('node-redshift');
const config = {
user: 'user',
database: 'database',
password: 'password',
port: port,
host: 'hostname',
};
// The values passed in to the options object will be the difference between a connection pool and raw connection
var redshiftClient = new Redshift(config, {rawConnection:true});
exports.handler = async (event) => {
return new Promise(function(resolve, reject){
redshiftClient.connect(function(err){
if(err) throw err;
else{
redshiftClient.parameterizedQuery('SELECT * FROM customer where user_id = $1', [event.user_id], {raw: true}, function(err, data){
if(err) throw reject(err);
else{
resolve(data);
redshiftClient.close();
}
});
}
});
});
};
I am using Knex query builder and Postgres as my data base.
I need to get the output from a raise notice or raise info from a Postgres function or procedure.
raise notice 'This is my Log';
we need to set notice after pool created, for that we need to use afterCreate function.
for your references
https://github.com/knex/knex/issues/4241
The sample code is below.
var pConn = {
host: 'SERVER',
port: 'PORT',
user: 'USER',
password: 'PASSWORD',
database: 'DATABAENAME'
};
var query = "select * from pglog_test()";
try {
const knex = require('knex')({
client: 'pg',
connection: pConn,
pool: {
afterCreate: function (conn, done) {
conn.query('select 1 as result', function (err, result) {
conn.on('notice', function (msg) {
// here we can get the pg procedure logs (raise notice/raise info)
console.log('logs from postgress' + msg);
});
done(err, conn);
});
}
}
});
// procedure calling
knex.raw(query).then(function (result) {
console.log(result);
}).catch(function (error) {
console.log(error);
});
} catch (error) {
console.log(error);
}
I am getting error Invalid object name dbo.tbl_user.while running a query on the Azure database from a nodejs app.My code which runs is following.
const pool = new sql.ConnectionPool(DatabaseConfig.config);
pool.connect().then(function () {
//6.
debugger;
var request = new sql.Request(pool);
//7.
request.query("select * from [dbo].[tbl_user]").then(function (recordSet) {
debugger;
console.log(recordSet);
sql.close();
}).catch(function (err) {
//8.
debugger;
console.log(err);
sql.close();
});
}).catch(function (err) {
//9.
debugger;
console.log(err);
});
My connection string is :
config: {
server: 'azureserver',
options: {
database: 'cccc',
encrypt: true,
port: 1433
},
user: 'ccc',
password: 'ccccc'
}
what is the mistake I am doing here? Thanks in advance for the help.
I tested your code on my side but did not reproduce your issue.
Here is my test code:
const sql = require('mssql')
const config = {
server: '***.database.windows.net',
database: '***',
user: '***',
password: '***',
options: {
database: '***',
encrypt: true,
port: 1433
}
}
const pool = new sql.ConnectionPool(config);
pool.connect().then(function () {
//6.
debugger;
var request = new sql.Request(pool);
//7.
request.query("select * from [dbo].[Student]").then(function (recordSet) {
debugger;
console.log(recordSet);
sql.close();
}).catch(function (err) {
//8.
debugger;
console.log(err);
sql.close();
});
}).catch(function (err) {
//9.
debugger;
console.log(err);
});
Query result on the portal:
Run query.js :
You need to make sure the table name is correct, it's not related to spelling case.
Hope it helps you.
In all tutorials I searched the web, they print the rows/result using console.log the from the mysql query. In my case, I want to store that value to manipulate it in a function. The problem is that node doesn't wait. Can you help me in catching the rows in such example? How can I store the rows in a variable?
con.connect(function (err) {
if (err) throw err;
con.query("SELECT * FROM customers", function (err, result) {
if (err) throw err;
console.log(result);
});
});
I tested one of the solutions but I got [] when trying to get result/rows out of the function. I want to use the result in the savedServices: [] see below:
Give a fresh start with a simple project
Follow below commands
mkdir testdb
cd testdb
npm install --save mysql
npm install --save async
Create a test.js file
vim text.js
Now put below code in the test.js file
var mysql = require('mysql');
var async = require('async');
var con = mysql.createConnection({
host: "localhost",
database: "mydb",
user: "user",
password: "pass"
});
var myrows = [];
async.waterfall([
function(callback) {
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = 'select * from users limit 5';
con.query(sql, function (err, result) {
if (err) throw err;
result.forEach(function(item) {
//console.log(item);
myrows.push(item);
});
callback(null, myrows);
});
});
}
],
// optional callback
function(err, myrows) {
console.log("myrows:::");
console.log(myrows);
});
now test the file
node test.js
You should see results like this:
Connected!
Result: [object Object],[object Object],[object Object],[object Object],[object Object]
Hope this helps!
I am trying to fetch a table, whose name is defined in another table and return the results.
I have defined a database module with these methods:
var mysql = require("mysql");
var config = require("../config/database.js");
module.exports = {
pool: mysql.createPool({
host: config.host,
user: config.user,
database: config.database,
password: config.password,
}),
getConnection: function(callback) {
this.pool.getConnection(callback);
},
fetch: function(table, callback) {
this.getConnection(function(err, connection) {
if(err) {
callback(err);
} else {
connection.query("SELECT * FROM ??", table, callback);
connection.release();
}
});
},
};
I am trying to fetch the table and then the "sub" table like so:
db.fetch("foo", function(err, rows) {
if(err) {
throw err;
} else {
_.each(rows, function(row) {
db.fetch(row.tbl, function(err, result) { console.log(err, result); });
});
}
});
However the inner-fetch callback is never called.
Why is this? I suspected I needed to close the first connection before establishing a second so I tried taking the each loop outside but encountered things becoming out of sync.