سؤال

I'm trying to use forge.prefs to store variables but nothing seems to be saving.

I am using the following:

var all = forge.prefs.keys(function(keysArray){ return keysArray},
                           function(content){return content});

forge.logging.log(all);

But this always returns undefined I am also having the same issue with setting, nothing seems to be working.

var set = forge.prefs.set(key,value,function(){},function(content){return content;});
forge.logging.log(set);

Again returns undefined and no error or anything.

Am I doing something wrong?

Using the docs found here

UPDATE

I won't to do something like the following:

var get = forge.prefs.get(key, 
                    function(value){return value;}, 
                    function(error){forge.logging.log(error);
});

if(get){
// do something here
}else{
// do something here
}
هل كانت مفيدة؟

المحلول

Those methods are asynchronous which means that subsequent code might be executed before the callback success and error functions are returned. All subsequent code that relies on the outcome of this should run within the callback:

forge.prefs.keys(function(keysArray){ // success
  forge.logging.log(keysArray);
  // subsequent code relying on a positive outcome here ...
},
function(error){ // error
  forge.logging.log('Something bad happened: ' + error);
});

forge.prefs.set(key, value, function(){ // success
  forge.logging.log(key + ' was properly set);
  // subsequent code relying on a positive outcome here ...
},
function(error){ // error
  forge.logging.log('Something bad happened: ' + error);
});

UPDATE: use this to achieve the updated part of your question:

var get;
forge.prefs.get(key, function(value){ // success
  get = value;
  forge.logging.log('Found value: ' + get);
  // subsequent code here ...
},
function(error){ // error
  forge.logging.log('Something bad happened: ' + error);
});

نصائح أخرى

It appears you're not returning anything in the forge.pref.set success function. Your return content; is in the error function so if it's not throwing an error you won't return anything.

Try this:

var set = forge.prefs.set(key,value,function(){ return value; },function(content){return content;});

forge.logging.log(set);

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top