0

Unable to assign a value in a javascript function while fetching from sqlite.

For Eg: This code in not working

function bookContent(dtb) {

    var bkContent = '';     

    dtb.all("SELECT count(*) ttlRw FROM books;", function(err, row) {

        if (err)
        bkContent = "Error SQL";
        else
        {           

            if(parseInt(row[0].ttlRw)>0)
            {
                bkContent = row[0].ttlRw + " rows";
            }
            else
            bkContent = 'No rows found!';                   
        }

    });

    return bkContent;

}

It's returns an empty value.

This is working

function bookContent(dtb) {

    dtb.all("SELECT count(*) ttlRw FROM books;", function(err, row) {

        if (err)
        document.getElementById("bookCnt").innerHTML = "Error SQL";
        else
        {           

            if(parseInt(row[0].ttlRw)>0)
            {
                document.getElementById("bookCnt").innerHTML = row[0].ttlRw + " rows";
            }
            else
            document.getElementById("bookCnt").innerHTML = 'No rows found!';                    
        }

    });

}

I want the first one work. Please tell me, where i am wrong.

0

2 Answers 2

1

You're returning a value before the asynchronous dtb.all() finishes. You need to pass a callback to bookContent and call it when the database query finishes.

var globalBkContent = "";  
function bookContent(dtb, callback) {
    dtb.all("SELECT count(*) ttlRw FROM books;", function(err, row) {

        if (err)
        return callback("Error SQL");
        else
        {           

            if(parseInt(row[0].ttlRw)>0)
            {
                return callback(false, row[0].ttlRw + " rows");
            }
            else
            return callback("No rows found!")           
        }

    });
}

var dtb = new DatabaseInstance();//Or whatever your dtb variable is defined as.
bookContent(dtb, function(err, bkContent) {
    if(err) throw(err);
    globalBkContent = bkContent;
    console.log(bkContent); //Here your bkContent & globalBkContent are both accessible and have the correct values. 
    //Make sure you don't do anything with globalBkContent until it is set by this function.
})

//If I access globalBkContent outside the callback above then it will still be an empty string.
Sign up to request clarification or add additional context in comments.

10 Comments

Thanks @forrestmid for your reply. But still how can i get the bkContent value outside bookContent(); Like assign to a global variable.
@Jay Your question doesn't ask anything about a global variable. I'll modify my answer to allow for a global variable, but your primary issue is because you're returning the function before the asynchronous portion has finished.
@Jay Additionally, you can't do anything with bkContent until your callback is returned, simply because it hasn't been set yet. You need to read up on how asynchronous functions work in Node if you want to be able to program in it. It's vitally necessary to know how to nest asynchronous functions in order to accomplish a goal in the programming language.
Thanks @forrestmind. Thanks for the help.
SORRY! It's not working
|
0

I have basic knowledges Nodejs, but I think it returns empty value because in the first code your return bkContent out of the dtb.all() function. But in the second code, you write in the function.

4 Comments

Thanks @Teymur Mardaliyer Lennon for your reply. I tried it inside the dtb.all. But then the function returns an 'undefined' msg;
@Jay may be it will help you? stackoverflow.com/questions/25399725/… or stackoverflow.com/questions/18361930/… or you need just return value?
Also you can test seems like this var result = dtb.all("SELECT count(*) ttlRw FROM books;", function(err, row) {...... return bkContent; }); console.log(result); hm?
i tried it. but it's returns a DB object. Not the values returns from bkContent. I will try the links you specified above. Thanks once again for the help.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.