Now I want to bind the data responded with function "getPresentationFilterValues" with filterItem.
How can I get the data of filter Item when calling the callback function with promise? Following is the code.
$q.all(presentationPromiseList).then(
function(response){
for(var i=0; i<response.length; i++){
config = self.bindAppPresentation(response[i], {config:config});
var filtering = config.configuration.filtering;
for(var i = 0; i< filtering.length; i++){
for(var j = 0; j < filtering[i].filters.length; j++){
var filterItem = filtering[i].filters[j];
if(filterItem.source == 'dynamic'&& !filterItem.options){
responsePromise = reveal.getPresentationFilterValues(filterItem);
responsePromise.then(function(response){
}).catch(function(ex){});
}
}
}
}
console.log("App presentation bind");
return 'param1';
},
function(response){
return $q.reject(new Error("failed to get app presentation: " + response.data.message));
}
)
Something like this:
const outsideData = 1
someFunctionWhichReturnsPromise().then(function() { return outsideData })
Related
I am getting an error in console while i am trying to get a specific value from json data. This is the error:
'Uncaught TypeError: Cannot read property 'processen_id' of undefined'
Here is my code:
$.get("/getProces", function (data) {
if (data.error) {
} else {
for (var i = 0; i <= data.message.length; i++) {
var obj = data.message[i]
console.log(obj.processen_id)
}
}
})
}
This is what i get when i log (data):
You have a mistake in your code in the for loop
<= instead of <
$.get("/getProces", function (data) {
if (data.error) {
} else {
for (var i = 0; i < data.message.length; i++) {
var obj = data.message[i]
console.log(obj.processen_id)
}
}
})
}
The error message means that obj is undefined. That means, that data.message[i] gets an undefined value. The problem is the loop. You get an i that is larger then the array. Change <= to <:
for (var i = 0; i < data.message.length; i++) {
var obj = data.message[i]
console.log(obj.processen_id)
}
index out of range: for (var i = 0; i <= data.message.length; i++) { should be for (var i = 0; i < data.message.length; i++) { instead.
you can also optimize your code in this way:
$
.get("/getProces")
.then((res) => res.error ? Promise.reject(res) : Promise.resolve(res))
.then((data) => {
for (var i = 0; i < data.message.length; i++) {
var obj = data.message[i]
console.log(obj.processen_id)
}
})
I've a situation where I've to get the object based on each item in for loop, after getting that object I've to make another for loop to get object for that item. How to i create a promise for this? For reference, you can check my code below:
for(var i = 0 ; i < children.length ; i++)
{
firebaseref = firebase.database().ref(children[i]);
var parentRef = firebaseref.once('value').then(function(snapshot){
var childRef;
if(snapshot.val().userRef!=undefined){
var keys = Object.keys(snapshot.val().userRef);
for(var i = 0; i < keys.length ; i++){
if(Is_static==true){
childRef = firebase.database().ref(keys[i]);
}
else{
childRef = firebase.database().ref(keys[i]);
}
childRef = childRef.once('value').then(function(snapshot){
var email = snapshot.val().email;
var mob = snapshot.val().mob;
if(email!=undefined && email!=""){
if(emails.indexOf(email)==-1)
emails.push(email);
}else if(mob!=undefined && mob!=""){
if(mobile.indexOf(mob)==-1)
mobile.push(mob);
}
});
promises.push(childRef);
}
}
});
promises.push(parentRef);
}
Promise.all(promises).then(function(results)
{ callAjax({EmailColl:emails,MobColl:mobile,Subject:sub,Message:message});
});
responseText like "insert-90RS252,insert-90RS262,insert-90RS232"
function(responseText) {
var res = responseText.split(",");
for (var i = 0; i < res.length; i++) {
var res2 = res[i].split("-");
if (res2[0] === "insert") {
alert(res2[0] + i);
}
}
}
When alert:
insert1 and insert2
Not begin for step 0.Please help.
if(res2[0]==="insert") should be if(res2[1]==="insert") because 'insert' will be the second element in res2 array.
It is Working....
var Text = "insert-90RS252,insert-90RS262,insert-90RS232";
var res = Text.split(",");
for (var i = 0; i < res.length; i++) {
var res2 = res[i].split("-");
if (res2[0].trim() === "insert") {
alert(res2[0] + i);
}
}
Here is my code :
for (var i = 0; i < list.length; i++) {
$.when(
$.ajax({url: "./dorequest.php?id=" + list[i],
success: function(response){ jsonFriendlist = response; }}) ).done(
function(jsonFriendlist) {
var friendListObject = JSON.parse(jsonFriendlist);
if (!jQuery.isEmptyObject(friendListObject)) {
var rawListFriend = friendListObject.friendslist.friends;
for (var j = 0; j < rawListFriend.length; j++) {
console.log(i);
playerLinkList[i].listFriends.push(rawListFriend[j].id);
}
}
}
);
}
Basicly I try to update a part of an object in the array list sending a request by item i that array. But the code fail because every call to the "done" function i done with i = 13 (which is list.lenght).
My understanding is that since the request take time to be send an retrieve the result and since it's done async when the done function is call the main thread is already out of the forso i = 13.
My question is how can I manage to "freeze" i by passing it by copy when the ajax request is sent ?
Thanks,
Using a closure solved the issue.
for (var i = 0; i < playerIds.length; i++) {
// Pass the parameter "i" to the done functions
(function(index) {
var dfd = $.ajax({url: "./dorequest.php?id=" + playerIds[i] + "&requesttype=friendlist",
success: function(response){ jsonFriendlist = response; }});
dfd.done(
function(jsonFriendlist) {
var friendListObject = JSON.parse(jsonFriendlist);
if (!jQuery.isEmptyObject(friendListObject)) {
var rawListFriend = friendListObject.friendslist.friends;
for (var j = 0; j < rawListFriend.length; j++) {
playerLinkList[index].listFriends.push(rawListFriend[j].steamid);
}
}
}
);
})(i);
}
I have been familiarizing myself with javascript closures and ran across this article
http://blog.morrisjohns.com/javascript_closures_for_dummies.html
Due to the closure, Example 5 does not work as expected. How would one modify
result.push( function() {alert(item + ' ' + list[i])} );
to make the code work?
function buildList(list) {
var result = [];
for (var i = 0; i < list.length; i++) {
var item = 'item' + list[i];
result.push( function() {alert(item + ' ' + list[i])} );
}
return result;
}
function testList() {
var fnlist = buildList([1,2,3]);
// using j only to help prevent confusion - could use i
for (var j = 0; j < fnlist.length; j++) {
fnlist[j]();
}
}
testList();
Thanks!
function buildList(list) {
var result = [];
for (var i = 0; i < list.length; i++) {
(function(i) {
var item = 'item' + list[i];
result.push(function() {
alert(item + ' ' + list[i]);
});
})(i);
}
return result;
}
You need the i and item from the different functions to be separate variables in different scopes (so they can have different values instead of being shared). Since Javascript only has function scope you need to create a wrapper function to contain these variables
function buildList(list) {
var result = [];
function make_f(item, i){
//item and i are now private variables of make_f
//and wont be shared by the returned closure
// the other variable in scope (list) is not shadowed so
// it is still shared but that is not a problem since we
// never change its value.
return function() { alert(item + ' ' + list[i]) };
}
for (var i = 0; i < list.length; i++) {
var item = 'item' + list[i];
result.push( make_f(item, i) );
}
return result;
}
You can also do the same thing with an anonymous function that is immediately invoked (the (function(){}()) pattern).
function buildList(list) {
var result = [];
for (var i = 0; i < list.length; i++) {
var item = 'item' + list[i];
result.push( (function (item, i){
return function(){function() {alert(item + ' ' + list[i])};
})(item, i) );
}
return result;
}
Put it in... another closure!
function buildList(list) {
var result = [];
for (var i = 0; i < list.length; i++) {
result.push((function(item, listItem){
return function() {
alert(item + ' ' + listItem);
};
})('item' + list[i], list[i]));
}
return result;
}
function testList() {
var fnlist = buildList([1,2,3]);
// using j only to help prevent confusion - could use i
for (var j = 0; j < fnlist.length; j++) {
fnlist[j]();
}
}
testList();
var has a function scope but if you use let then you will get block scope.
So I would do:
for (let i = 0; i < list.length; i++) and let item...