Loopback findone function - javascript

I am using loopback on server side of my application , to fetch and validate a data from database I'm using findOne method which is having a callback function. I wanted to get run the callback function as soon as the findone function is executed, The code i have written is working but i want to avoid usage of async-await. Any other alternative for this?
What I tried
function validId(req) {
const filter = {
where: {
ID: req.id,
}
};
//
const result = await model.findOne(filter);
if (result) {
return true;
} else {
return false;
}
}
module.exports = function () {
return async function validateTenant(req, res, next) {
var id = false;
if (req.url.includes("XYZ")) {
id = await validId(req)
}
//
if (id || !req.url.includes("XYZ")") {
next();
} else {
res.writeHead(404, { "Content-Type": "text/html" });
var html = fs.readFileSync(
"error.html"
);
res.end(html);
}
};
};

you could use the .then() function of the promise
model.findOne(filter).then((result)=>{
//execute the rest of the function that need to be executed after the findOne.
});
// The code will continue to execute while model.findOne is doing it's thing.
But if you want to wait for the FindOne to give a result without using await or the .then it is not possible unless you make a wrapper of findOne or your BDD package have a synchrone findOne

Related

Javascript Return Value in graphql

I have an apollo-server setup in my backed, I want to use a post request API inside one of my resolvers and then use the response from that API to return it to my client. but the problem am having is the return statement is running before the API response get returned. there is my code sample bellow.
module.exports = {
Mutation: {
checkFace: async () => {
console.log("Checking.....");
let confidence;
var parameters = {
image_url1: "link to image 1",
image_url2: "link to image 2",
};
facepp.post("/compare", parameters, function (err, res) {
if (!err) {
confidence = res.confidence;
} else {
console.log(err);
}
});
return confidence
},
},
};
That's because the faceapp.post is probably running asynchronously. Your checkFace function is correctly used as async that's fine, but inside where you call the POST you should await the response and then return it
confidence = await res.confidence;
Also when using the function make sure you await for it to finish, so call it with
let someResponse = await Mutation.checkFace();
console.log(someResponse);
or
Mutation.checkFace().then(response => {
console.log(response);
});
Whichever you prefer depending on your situation.
https://nodejs.dev/learn/modern-asynchronous-javascript-with-async-and-await

How to get promise.response to return an API’s response?

TLDR: my promise.response needed to be called within both the API call and the promise.
I am attempting to get a return value from an API call via a Promise for a simple Express.js server.
This seems to be a topic of a lot of questions, but I have yet to successfully adapt an implementation to this case. I've also tried:
placing the API call within resolve()
async/wait implementations (willing to revisit)
Here's the basic structure of the code in question. There's a comment above the section where the trouble probably is.
Promise
const externalModule = require('<route to module>');
let promise = new Promise(function(resolve,reject) {
// This is probably where the problem is
let returnValue = externalModule.apiCall(parameters);
resolve(returnValue);
});
promise.then(function(returnValue) {
console.log(returnValue);
});
External Module
module.exports = {
apiCall: function(parameters) {
apiCall(
parameters,
function(err, response) {
if (err) {
console.error(err);
return;
} else {
console.log("success");
return response
}
}
)
}
};
If the code were working properly, we'd see two strings. One from inside the API call ("success") and another from it's return value. Because undefined is appearing before "success," we know that the resolve function has fired before the function above it has returned.
Logs from the Shell
> undefined
> "success"
You aren't providing a way to use the response from the api call. Convert that toa promise and then use it.
module.exports = {
apiCall: function(parameters) {
return new Promise((res, rej) => {
apiCall(
parameters,
function(err, response) {
if (err) {
rej(err);
} else {
res(response);
}
}
)
});
}
};
Then use it like so
let promise = externalModule.apiCall(parameters);

async not waiting for await in callback to finish

Im still a rookie with async and awaits, but this is what Im working with.
async function _getImageURLs(folder) {
if (!id) return;
foo.AWSS3Helper.GetImageUrls(so, function (imageUrls) {
return await imageUrls;
});
}
The fuction _getImageURLs() is still returning without waiting for the AWSS3Helper.GetImageUrls finishes. Im guessing it has something to do with being inside a callback function. Does the callback need to be async as well?
Use await on the function not on the return statement
async function _getImageURLs(folder) {
if (!id) return;
foo.AWSS3Helper.GetImageUrls(so, await function (imageUrls) {
return imageUrls;
});
}
As far as i see there are no promises here, you have a callback based mechanism on the api so using async/await seems pointless
you should pass a callback to the method and invoking it as soon as you have the result available
function _getImageURLs(folder, callback) {
if (!id) callback();
foo.AWSS3Helper.GetImageUrls(so, function (imageUrls) {
callback(imageUrls);
});
}
_getImageURLs("somefolder", function(result) {
console.log(result);
// do whatever you want here with the result
};
Try it with observables. Something like this...
function _getImageURLs(folder) {
if (!id) return;
const imageUrlObservable = of(foo.AWSS3Helper
.GetImageUrls(so, function (imageUrls) {
return imageUrls;
})
);
imageUrlObservable.subscribe({
next(imageUrL) { console.log(imageUrL) },
complete() { console.log('Finished sequence'); }
});
}

Run one function after another in javascript

I am using javascript to use the facebook send api.
function sendmessage(callback) {
for (i = 0; i < recipientId.length; i++) {
var messageData = {
recipient: {
id: recipientId[i]
},
message: {
text: messageText
}
};
callSendAPI(messageData, pagetoken, id_notsent);
}
return callback( );
}
function sendstatus() {
if (id_notsent.length == 0) {
res.statusCode = 200;
res.message = "Successfully sent generic message to all recipients";
} else {
res.statusCode = 400;
res.message = "Unable to send message to all users. Message not sent to recipients : " + id_notsent.toString();
};
resp.send(res);
}
sendmessage(sendstatus);
What i am trying to do is to update the id_notsent variable inside the sendmessage function which will basically contain user id correspoding to which message couldn't be send and then sending back the response accordingly using sendstatus function. but the problem is that the callback in sendmessage is getting called before the callSendAPI function is completed.
I suspect callSendAPI is return some sort of Promise (or has a callback that you can turn into a Promise).
The structure your sendMessage() function should then be around the lines of
const promises = recipentId.map( id => {
...
return callSendAPI(messageData, pagetoken, id_notsent);
});
Promise.all(promises).then(callback);
Basically: get promises for all your calls, wait for them to complete using Promise.all then callback
You have multiple soluce here :
Using async/await ES8 pattern.
function async sendmessage() {
for (i = 0; i < recipientId.length; i++) {
var messageData = { ... };
await callSendAPI(messageData, pagetoken, id_notsent);
}
return ...;
}
Create a recursive function that's gonna call one by one the callSendAPI.
For example :
function sendmessage({
recipientId,
callback,
i = 0,
rets = [],
}) {
// our work is done
if (i >= recipientId.length) return callback(rets);
const messageData = { ... };
// Perform one request
callSendAPI(messageData, pagetoken, id_notsent, (ret) => {
// Call next
return sendmessage({
recipientId,
callback,
rets: [
...rets,
ret,
],
i: i + 1,
});
});
}
You can use either callback (what you are doing now), or either Promise.

Return Meteor.http results in method

I have a Meteor method that wraps around an http.get. I am trying to return the results from that http.get into the method's return so that I can use the results when I call the method.
I can't make it work though.
Here's my code:
(In shared folder)
Meteor.methods({
getWeather: function(zip) {
console.log('getting weather');
var credentials = {
client_id: "string",
client_secret: "otherstring"
}
var zipcode = zip;
var weatherUrl = "http://api.aerisapi.com/places/postalcodes/" + zipcode + "?client_id=" + credentials.client_id + "&client_secret=" + credentials.client_secret;
weather = Meteor.http.get(weatherUrl, function (error, result) {
if(error) {
console.log('http get FAILED!');
}
else {
console.log('http get SUCCES');
if (result.statusCode === 200) {
console.log('Status code = 200!');
console.log(result.content);
return result.content;
}
}
});
return weather;
}
});
For some reason, this does not return the results even though they exist and the http call works: console.log(result.content); does indeed log the results.
(Client folder)
Meteor.call('getWeather', somezipcode, function(error, results) {
if (error)
return alert(error.reason);
Session.set('weatherResults', results);
});
Of course here, the session variable ends up being empty.
(Note that this part of the code seems to be fine as it returned appropriately if I hard coded the return with some dummy string in the method.)
Help?
In your example Meteor.http.get is executed asynchronously.
See docs:
HTTP.call(method, url [, options] [, asyncCallback])
On the server, this function can be run either synchronously or
asynchronously. If the callback is omitted, it runs synchronously and
the results are returned once the request completes successfully. If
the request was not successful, an error is thrown
Switch to synchronous mode by removing asyncCallback:
try {
var result = HTTP.get( weatherUrl );
var weather = result.content;
} catch(e) {
console.log( "Cannot get weather data...", e );
}
Kuba Wyrobek is correct, but you can also still call HTTP.get asynchronously and use a future to stop the method returning until the get has responded:
var Future = Npm.require('fibers/future');
Meteor.methods({
getWeather: function(zip) {
console.log('getting weather');
var weather = new Future();
var credentials = {
client_id: "string",
client_secret: "otherstring"
}
var zipcode = zip;
var weatherUrl = "http://api.aerisapi.com/places/postalcodes/" + zipcode + "?client_id=" + credentials.client_id + "&client_secret=" + credentials.client_secret;
HTTP.get(weatherUrl, function (error, result) {
if(error) {
console.log('http get FAILED!');
weather.throw(error);
}
else {
console.log('http get SUCCES');
if (result.statusCode === 200) {
console.log('Status code = 200!');
console.log(result.content);
weather.return(result);
}
}
});
weather.wait();
}
});
There's not really much advantage to this method over a synchronous get in this case, but if you're ever doing something on the server which can benefit from something like an HTTP call running asynchronously (and thus not blocking the rest of the code in your method), but you still needs to wait for that call to return before the method can, then this is the right solution. One example would be where you need to execute multiple non-contingent gets, which would all have to wait for each other to return one by one if executed synchronously.
More here.
Sometimes asynchronous calls are preferable. You can use async/await syntax for that, and you need to promisify HTTP.get.
import { Meteor } from 'meteor/meteor';
import { HTTP } from 'meteor/http';
const httpGetAsync = (url, options) =>
new Promise((resolve, reject) => {
HTTP.get(url, options, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
Meteor.methods({
async 'test'({ url, options }) {
try {
const response = await httpGetAsync(url, options);
return response;
} catch (ex) {
throw new Meteor.Error('some-error', 'An error has happened');
}
},
});
Notice that meteor test method is marked as async. This allows using await operator inside it with method calls which return Promise. Code lines following await operators won't be executed until returned promise is resolved. In case the promise is rejected catch block will be executed.

Categories

Resources