I need to wait for the localStorage to retrieve a value before continuing my function... I read that I have to use "async/await" instructions but none of the examples I found really works.
My Code is
_ajustes.EsPrimeraVez().then (async (promAjuste)=> {
await console.log('TEST! en el then');
});
console.log('TEST! Post');
and inside "ajustes":
public EsPrimeraVez() {
console.log('TEST! entrando en primeravez');
return this.storage.get('primeravez');
but it doesn't wait to the result to continue... is this possible?
Thanks and happy new year to all!
Why are you using prmoise/then syntac if you want to wait to retrieve value from a function. Below code will run sequentially and once function is executed it will assign local storage value to variable 'mystoragevalue'.
var mystoragevalue = _ajustes.EsPrimeraVez();
console.log('TEST! Post');
and inside "ajustes":
public EsPrimeraVez() {
console.log('TEST! entrando en primeravez');
return this.storage.get('primeravez');
}
Hope this helps...Happy coding...!!!
Related
I am struggling to find a solution to proceed with my program when a listener completes.
So my main goal is it to return a queryParameter of the DeepLink that leads back to my app. I already found out how to get the params with uni_links :
String function(){
linkStream.listen((String? link) async {
if (link!.startsWith(redirectUri)) {
param = Uri.parse(link).queryParameters['code'].toString();
print(param);
}
});
}
But now I want to return param instead of just printing it into the console. The problem is, because the return type of a listener is Future<void>, I am not able to just return the String without some kind of a workaround.
Is there a way to solve this?
userSchema.pre('save',async function(next){
//hash the password before saving user to database
next()
})
Hey guys I'm trying to understand the concept of middleware in mongoose. Assuming that I have an userSchema that I run the pre hook on to hash the password before saving the user to the database. On the surface, as far as I understand, the above code that I have will hash the password (not the important part for this question) and then call next() to signal that the function is done. However, I am trying to understand how things work under the hood. I want to know what is next() ? Can you guys walk me through an example of how everything works together under the hood from start to end once the code get executed or somehow help me to have a better understanding of this concept? Thanks
Short : with the pre method you can register listeners for certain events of your Schemas. So pre('save', callback) will fire whenever you save a document of said Model. pre means it will execute before the event, so it can be used (for example) to hash a password before saving it to the document.
However, you have several options to define them, see below :
The combination of using an async callback function and providing the next parameter is not necessary, you can either :
use normal callback with next parameter
the next parameter is a function provided to you by mongoose to have a way out, or to tell mongoose you are done and to continue with the next step in the execution chain. Also it is possible to pass an Error to next it will stop the execution chain.
schema.pre('save', function(next) {
// do stuff
if (error) { return next(new Error("something went wrong"); }
return next(null);
});
use async callback
Here the execution chain will continue once your async callback has finished. If there is an error and you want to break/stop execution chain you just throw it
schema.pre('save', async function() {
// do stuff
await doStuff()
await doMoreStuff()
if (error) { throw new Error("something went wrong"); }
return;
});
Straight from the docs : https://mongoosejs.com/docs/middleware.html#pre
Example
const { Schema, model } = require('mongoose');
const SomeSchema = new Schema ({
name : { type : String }
});
SomeSchema.pre('save', function (next) {
console.log('pre save fired!');
return next();
});
const Some = model('Some', SomeSchema);
console.log('before create');
const doc = new Some({ name : 'test' });
doc.save((err) => {
console.log('after saved');
});
This will output
before create
pre save fired!
after saved
i'm new on Flutter/Dart, on my code i'm retrieving some data from a table and map it to a List
My code works fine but i'd like to Handling errors & exceptions so i tried to use DatabaseException
This is my code:
I know that maybe the question is basic but i don't understand
why list is no valid inside "if (e.isNoSuchTableError()) {"
Thanks for help
MM
The list is defined inside a try block, which means its not in the scope of the catch block, that's why you cannot access it inside the catch block. You need to define it outside the try block to be able to access it:
List<Map> list = null;
try {
list = await db.rawQuery(cmd);
} on DatabaseException catch(e){
//access it
}
The same concept is used in methods:
void getData(){
List<String> listOfNames = List<String>();
}
void retrieveData(){
print(listOfNames); // Undefined name 'listOfNames' - line 10
}
I've very new to this but essentially I want to create a cleanup function that runs on the server that I can call at any time to reset various things like collections and sessions in one call.
I'm really very new but this is what I have so far. Can someone please help fill me in where I'm going wrong?
I am trying essentially to return two things (and many more in the future) at once. I've done some research on this but it's as far as I can fathom with my skill level at the moment.
It would be much appreciated. Thank you.
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
return Meteor.methods({
//Use this to emplty the form data
cleanUpForms: function() {
var cleanUpPhoneNumbers = orgPhoneNumbers.remove({});
var cleanUpEmailAddresses = orgEmailAddresses.remove({});
return {
cleanUpPhoneNumbers : cleanUpPhoneNumbers;
cleanUpEmailAddresses : cleanUpEmailAddresses;
}
}
});
});
}
By the way, the current error is for line :
cleanUpPhoneNumbers : cleanUpPhoneNumbers;
It states:
Unexpected token
I'm not sure if I'm doing this correctly. I essentially want it to run multiple cleanups in one go, all called from the client to the server with the above method. I hope that makes sense.
The unexpected token is likely for the ; at the end of the line. When building a JSON object, use a comma between the elements...
return {
cleanUpPhoneNumbers : cleanUpPhoneNumbers,
cleanUpEmailAddresses : cleanUpEmailAddresses
}
I think this will return the number of items that where removed. Is that what you are expecting?
Also, just in case you didn't know, you can run 'meteor reset' from the command line to erase ALL collections.
This is the fully adjusted code for any future reference which may help others. Thanks so much to FloatingCoder for the help.
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
return Meteor.methods({
removeAllNewOrgs: function() {
var PhoneNumbers = newOrgPhoneNumbers.remove({});
var Organsations = newOrgansations.remove({});
//If we want to return the data, to get around only being able to return one thing at a time we're return via an array. CLEVS!
return {
PhoneNumbers : PhoneNumbers,
Organsations : Organsations
}
}
});
});
}
I am using this code to update data of a Parse User, but the data is not updating nor am I able to retrieve the data.
I am able to do update and retrieve the data for a ParseObject class created by me. Can someone tell me where I am wrong on this?
Out of all the debugs I have set I am able to see only the 1st debug "parse GetSync function passed" and the rest are not printing.
var parseUser: ParseUser;
function FBtoParse(){
var query = ParseUser.Query;
query.GetAsync("Q0D9eBvRee").ContinueWith(GetSync);
Debug.Log("parse GetSync function passed");
Debug.Log("Object ID: "+ parseUser["objectId"]);
Debug.Log("username is : "+ parseUser["username"]);
Debug.Log("User Updated");
}
var GetSync = function (t:System.Threading.Tasks.Task.<ParseUser>){
parseUser = t.Result;
};
function OnGUI{
if (GUILayout.Button("Parse"))
{
FBtoParse();
Debug.Log("Pressed Parse");
}
}
Parse at the moment only allows ParseUsers to update their own object through authentication.
https://parse.com/docs/js/guide#users-security-for-user-objects