Reputation: 47
When I try to access a function, findpen() I am getting an error:
each key must be a number of string; got undefined.
Can someone please help? Is there any problem in declaring variable?
var mysql = require('../../node_modules/mysql');
describe ('protractor Demo', function() {
it ('login',function(done)
{
............
............
Console.log ('login sucess')
});
it('E2E',function(done)
{
var ofrecord = findPen();
browser.sleep(12000);
browser.element(by.css('div > input')).sendKeys(ofrecord);
browser.actions().sendKeys(protractor.Key.ENTER).perform().then(function()
{
console.log('reference # is searched sucessfully');
})
var accountnumber;
function findPen()
{
var connection = mysql.createConnection
({
host: 'host',
user :'user',
password :'password',
database:'database'
});
connection.connect(function(){
console.log('DB is connected');
});
connection.query("SELECT * from TEST where TEST Descrption='TCRC' LIMIT 1",function(err,rows)
{
if (!err)
{
console.log("result is :", rows[0].Tentacles);
accountnumber=rows[0].Tentacles;
console.log("output is :", accountnumber);
return accountnumber;
}
else
{
console.log("Error"+err)
}
});
}
Upvotes: 1
Views: 3895
Reputation: 899
Firstly, you have a typo in the word "Description" in your SQL query, and you're currently not handling the case when no rows are returned. Which could cause you problems.
Secondly, findPen() doesn't return anything - the connection query within findPen() returns accountNumber, but findPen() doesn't then do anything with that. You need to call and return the query function at the end of findPen().
As a very simple example, this code will produce the error when called as you have done:
var buzz;
function foo () {
var bar = function() {
buzz = 1;
return buzz;
}
}
It would just need modifying like this to return correctly:
var buzz;
function foo () {
var bar = function() {
buzz = 1;
return buzz;
}
return bar();
}
Upvotes: 2