1

I am developing ionic hybrid application. I am using $http to get value from server. Next, I will make a cordova sqlite query, inside the cordova query I want to insert my result from $http call from server and result of cordova query into my sqlite database. However, I can't get the value of $http return value in my cordova query. The following is my code:

$http({
    method: "post",
    url: "http://localhost/php2/get_channel.php",
    data: {
        user_name: usernameID
    },
    headers: { 'Content-Type': 'application/json' }
}).success(function(response) {
    for(i=0; i<response.length; i++){
        var channelNameQuery = "SELECT * FROM chat_friend WHERE channel_id=?"
        var channelNamePromise = $cordovaSQLite.execute(db, channelNameQuery, [response[i].ChannelName]).then(function (result){
            var ChannelQuery = "REPLACE INTO chat_channel (last_text, usernameID, chat_channel, chat_channel_name) VALUES (?,?,?,?)";
            $cordovaSQLite.execute(db, ChannelQuery, [response[i].LastText, usernameID,  response[i].ChannelName, result.rows.item(0).UserName]);
        })
    }
}).error(function(response) {
    console.log(response);
    console.log("failed");
});

I can't get response[i].LastText and response[i].ChannelName value inside $cordovaSQLite.execute() function.

Sorry for my poor language.

1
  • Show us response json data Commented Feb 15, 2017 at 10:12

1 Answer 1

1

The data you recive is mapped on response.data. Try looping thru your data by using angular.forEach(). Remember that response is mostly a object so you cant get response.length here. Please take a look at the $http AngularJS documentation.

$http({
    method: "post",
    url: "http://localhost/php2/get_channel.php",
    data: {
        user_name: usernameID
    },
    headers: { 'Content-Type': 'application/json' }
}).success(function(response) {
    angular.forEach(response.data, function (data) {
        var channelNameQuery = "SELECT * FROM chat_friend WHERE channel_id=?"
        var channelNamePromise = $cordovaSQLite.execute(db, channelNameQuery, [data.ChannelName]).then(function (result){
            var ChannelQuery = "REPLACE INTO chat_channel (last_text, usernameID, chat_channel, chat_channel_name) VALUES (?,?,?,?)";
            $cordovaSQLite.execute(db, ChannelQuery, [data.LastText, usernameID, data.ChannelName, result.rows.item(0).UserName]);
        })
    });
}).error(function(response) {
    console.log(response);
    console.log("failed");
});
Sign up to request clarification or add additional context in comments.

Comments

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.