0

THE PROBLEM I AM FACING VERY PECULIAR.THE VALUES WHAT I WANT IS VISIBLE,WHEN I GET IT AS MESSAGE FROM THE SERVER-WEB CONSOLE. BUT,WHEN I GIVE IT IN FOR LOOP,IT SHOWS ME AN ERROR OF 05-22 18:58:23.203: I/Web Console(29392): JSCallback Error: TypeError: Cannot read property 'value' of undefined at file:///android_asset/www/cordova-2.1.0.js:3727. THIS MEANS MY QUERY IS RIGHT.I HAVE COMMITTED SOME MISTAKE IN THE FOR LOOP FOR RETRIEVING DATA.

I am new to phone gap and using JavaScript to code.I have a doubt and a problem while executing it.I am using sqlite database in my project.

My table structure will be almost like this

id  cid values

1   1   value1
2   1   value2
3   1   value3
4   1   value4
5   2   value1
6   2   value2
7   3   value1
8   3   value2
9   3   value3

so I want all the values of the same cid,how should I code it?

My query:

tx.executeSql("SELECT cid,value FROM table where cid="+cid_value, [], querySuccess, errorCB,cid_value);

because,when I tried it the following manner,

var details = results.rows.length;
console.log("db test value:"+results.rows.item(cid_value).value);
for (var i=cid_value; i<details; i++)
{
cidinstance=results.rows.item(i).cid;
var valueinstance=results.rows.item(i).value;
document.getElementById("s"+i+"cause").innerHTML=valueinstance;
console.log("cid= "+cidinstance +   "valueinstance= "+valueinstance);
}

then when i=cid_value(cid_value=1) targeting cid=1, where we have four values I get just 3 values.but,if i put i=0,then I get 4 values

when i=cid_value(cid_value=2) targeting cid=2,i get the following

01-01 05:45:38.687: I/Web Console(2425): JSCallback: Message from Server: SQLitePluginTransaction.queryCompleteCallback('1104538538488000','1104538538491000', [{"value":"value1","cid":"2"},{"value":"value22","id":"6","cid":"2"}]); at file:///android_asset/www/cordova-2.1.0.js:3726.

The Problem is,I am getting the values when I get the message from the server as JS Callback. not from the for loop

Please,suggest me ways to solve the problem!.guide me to find a way out.

1 Answer 1

2

try this, it should work (I haven't tested it, in case let me know):

cid_value = "1";
try{
    tx.executeSql("SELECT cid , value FROM table where cid = ?;", [cid_value], success, failure);
}catch(e){
    console.log("error: "+e.message);
}
this.success = function(tx, results){
    if(results.rows.length > 0){
        for(var x = 0; x < results.rows.length; x++){
            var cidinstance = results.rows.item(x).cid;
            var valueinstance = results.rows.item(x).value;
            console.log("cid = "+cidinstance + " | valueinstance = "+valueinstance);
    }else{
        console.log("results length = 0");
    }
};
this.failure = function(tx, error){
    console.log("error: "+error.message)
}

I think your problem is related to the value you initialize your "i" ver in the for cycle. let it start from 0 to get every result of the resultset, instead of a subset of it.

Hope it helps

Sign up to request clarification or add additional context in comments.

9 Comments

yes,you are right.My problem is there. if i put i=0 as you say it will work only with cid=1 not with the rest of the cid values
with my code, if you use cid=2 does it return a resultset with length > 0 (enters the if statement in the success callback?)? does it throw an error?
it throws an error as 05-22 18:58:23.203: I/Web Console(29392): JSCallback Error: TypeError: Cannot read property 'value' of undefined at file:///android_asset/www/cordova-2.1.0.js:3727 When cid=2 and above
can you double check that you start your for loop from 0? If so, can you repost the updated code? That would be useful to better understand your problem. It seems like you're trying to get a value from a result's index that is not defined (ie: object length = 2, and you want to get the 3rd item in the object)
thank you, for the answer and especially this comment and these words can you double check ...you have helped me...thank you once again
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.