0

I have two arrays called one and two. one contains string values and two int values. I'm trying:

var messageObject = { 'One': one,
                       'Two': two};
var serializedJSON = JSON.stringify(messageObject);
var json = JSON.parse(serializedJSON);
alert(json.One);

I'm getting "Undefined", though the array is populated. They are receiving data from a database, like this:

db.transaction(function(transaction) {
    transaction.executeSql('SELECT * FROM aluno', [], function(transaction, results) {
        len = results.rows.length, i;
        for (i = 0; i < results.rows.length; i++) {
            one[i] = results.rows.item(i).fieldOne;
            two[i] = results.rows.item(i).fieldTwo;
        }            
    }, null);
});

See the code:

http://jsfiddle.net/U4C6r/2/

5
  • Please post the two arrays. Commented Apr 19, 2014 at 18:27
  • are you sure you defined one and two before defining messageObject ? Commented Apr 19, 2014 at 18:29
  • cannot reproduce Commented Apr 19, 2014 at 18:29
  • Seems to work fine here: jsfiddle.net/jfriend00/U4C6r Commented Apr 19, 2014 at 18:29
  • See the update, please. Is there any problem receiving data from database? Commented Apr 19, 2014 at 18:45

3 Answers 3

1

Edited Answer:

The problem seems to be with the DB-Query-Code. Either the query or the callback get never executed, see: http://jsfiddle.net/U4C6r/11/


Original Answer:

var messageObject = { 
  'One': one,
  'Two': two
};

If you define your keys as strings, you should access them as such:

console.log(json['One']);

If you'd like to have them as properties on the object, you should do:

var messageObject = { 
  One: one,
  Two: two
};

Then you could access the data in a chaining fashion, like you want to:

console.log(json.One);

You should also see the difference here and in your IDE by the specific Syntax-Highlighting - see it? :)

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

1 Comment

Okay, so this wasn't the problem here - sorry. It seems your DB-Execution-Callback is never executed, see: jsfiddle.net/U4C6r/4
0

EDIT to updated: put breakpoint as you read the values from webSQL and check the structure of object you get within loop inspect this obj results.rows.item(i)

Also change your script to this. I am assuming you have global var one = []; and var two = []; declared.

var dynamicArrayContainer = { one : [], 
two : []};
db.transaction(function(transaction) {
transaction.executeSql('SELECT * FROM table', [], function(transaction, results) {
    for (var i = 0; i < results.rows.length; i++) {
        dynamicArrayContainer .one.push(results.rows.item(i).fieldOne);
        dynamicArrayContainer .two.push( results.rows.item(i).fieldTwo);
    }            
}, null);
});

Try this, put your values directly, as debug exercise

var serializedJSON = JSON.stringify(dynamicArrayContainer);
var json = JSON.parse(serializedJSON);
alert(json.one);

4 Comments

See the update, please. Is there any problem receiving data from database?
I try, it worked. But I need creating the array dinamically. Would you try it, please?
I didn't know exists that "push" method. Thank for that. But still doesn't work out. Can you have a look at the code? jsfiddle.net/U4C6r/10
I tried it in my real code. It worked properly. The JSFiddle my has some problem with databases, I don't know. Thanks!
0

Try this:

var messageObject = { One: 'one',
                       Two: 'two'};
var serializedJSON = JSON.stringify(messageObject);
var json = JSON.parse(serializedJSON);
alert(json.One);

Good luck :)

Update: Enquirer edited the query after this reply was posted. This reply pertains to the original query (ie the first part of the current query)

4 Comments

See the update, please. Is there any problem receiving data from database?
Why was this answer marked down? At the time I posted, it corresponded to the (unedited) query. Query was edited afterwards. The mark-down process seems rather supercilious in addition to discouraging further participation.
Actually, I use to mark all up, just for thanking the response.
Thank you LIUFA & MCGBra ...TBH, I am becoming increasing hesitant in replying. I know it is not your fault :)

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.