Fetch API Doesn't send data on first call - NestJs - node.js

I have an API in NestJs which is not sending data on the first hit. However, on hitting it again it sends the desired data. I am guessing the API returns before the internal processing is done.
How to stop this. Is sleep a good option for this?
Or is there any other way to do this?
#Post("load")
#UseGuards(AuthGuard("jwt"))
async load(#Req() body: any)
{
const organizationId = body.user.organizationId;
const userId = body.user.userId;
if ("brandIds" in body.body)
{
await this.userService.onBoardUser(userId);
}
var settings = await this.settingsService.fetchLayout(organizationId, "home");
settings.forEach(async (element) =>
{
var parsedElement = JSON.parse(JSON.stringify(element));
var innerContent = await this.fetchContent(parsedElement.method, organizationId, userId);
var template = parsedElement.content[0];
let formattedItem = {};
innerContent.forEach((item) =>
{
try
{
formattedItem = template;
Object.keys(template).forEach((key) =>
{
if (template[key]!= "" && key != "type")
{
formattedItem[key] = eval(template[key]);
}
});
parsedElement.content.push(formattedItem);
formattedItem = null;
}
catch(err)
{
}
});
this.response.data.push(parsedElement);
innerContent = null;
template = null;
formattedItem = null;
parsedElement = null;
});
return(this.response);
}

looks like your main problem here is that your using async/await inside foreach which isnt working.
Use it like this:
for (const setting of settings) {
... your async code here.
}

Related

How to speed up Fetching Google Place and Photos

I currently have the following code to fetch matching Google Places according to a received query as shown below:
async function searchGoogleBusiness(req, res) {
let { name } = req.query;
const apiKey = process.env.API_KEY;
const searchUrl = `https://maps.googleapis.com/maps/api/place/textsearch/json?query=`;
try {
let { data } = await axios.get(`${searchUrl}${name}&key=${apiKey}`)
let { status, error_message, results } = data;
if (status === 'OK') {
let businessResults = [];
if ((results ?? []).length > 0) {
for (let business of results) {
let businessDetails = {
....
}
if ((business.photos ?? []).length > 0) {
let { width = 1200, height = 1200, photo_reference } = business.photos[0];
let photoUrl = `https://maps.googleapis.com/maps/api/place/photo?photoreference=${photo_reference}&sensor=false&maxheight=${height}&maxwidth=${width}&key=${apiKey}`
try {
let businessPhotoResponse = await axios.get(photoUrl, { responseType: 'arraybuffer' });
let imageBuffer = businessPhotoResponse.data;
let base64Image = Buffer.from(imageBuffer, 'binary').toString('base64');
businessDetails.photo = `data:${businessPhotoResponse.headers['content-type']};base64,${base64Image}`;
} catch (e) {
businessDetails.photo = business.icon;
}
} else {
businessDetails.photo = business.icon;
}
businessResults.push(businessDetails);
}
}
...//Omitted
}
...//Omitted
} catch (e) {
...//Omitted
}
}
As you can immediately notice, the function takes forever to return when the results are more than 5 and the reason is because I'm looping through each business to make another api call to fetch each photo.
I don't like this approach at all.
This idea of making another network call using photoReferences is really affecting my site speed and basically just makes my users angry.
Is there no way to automatically fetch the photo urls along just in the first request?

What is the best way to execute various queries on request object in mongoose

I didn't know what would be the best title to describe my problem statement here. I went through an online course where the instructor chained methods on the request object to carry out different queries.
I do not know (the code is below) if this is the best way to do it. Honestly, I am having trouble understanding it completely.
The following is the code to display all tours in the website:
const Tour = require("../models/tourModel");
class APIFeatures {
constructor(query, queryString) {
this.query = query;
this.queryString = queryString;
}
filter() {
const queryObj = { ...this.queryString };
const excludedFields = ["page", "sort", "limit", "fields"];
excludedFields.forEach((el) => delete queryObj[el]);
// 1B) Advanced filtering
let queryStr = JSON.stringify(queryObj);
queryStr = queryStr.replace(/\b(gte|gt|lte|lt)\b/g, (match) => `$${match}`);
this.query = this.query.find(JSON.parse(queryStr));
// console.log(this.query);
return this;
}
sort() {
if (this.queryString.sort) {
const sortBy = this.queryString.sort.split(",").join(" ");
this.query = this.query.sort(sortBy);
} else {
this.query = this.query.sort("-createdAt");
}
return this;
}
paginate() {
const limit = 2;
const skip = 2;
this.query = this.query.skip(skip).limit(limit);
return this;
}
}
The controller function to get all the tours is as follows:
const getAllTours = async(req,res) => {
try{
const features = new APIFeatures(Tour.find(), req.query).sort().paginate();
const tours = await features.query;
// ... return the response
}
catch(err){
// do something if err
}
I don't understand why the instructor passed Tour.find() as a parameter in
const features = new APIFeatures(Tour.find(), req.query).sort().paginate();
In the filter() method there is already
this.query = this.query.find(JSON.parse(queryStr));
Would'nt this result in Tour.find().find()?
Is it necessary to define a class and chain the methods like this on the req object? Are there any better ways?

Best practice using promises chaining in node JS

I'm little bit confusing in promises. first, I have some ugly code like this:
async function presence(ctx) {
try {
var prsenceData = [];
var isSuccess = Boolean(false);
var ckFilePath = "./somepath/cookie.json";
if (!fs.existsSync(ckFilePath)) {
await menuLogin.login(ctx).then(login => {
isSuccess = Boolean(login[0].status);
myCk.saveCookies(login[0].cookies, ckFilePath);
if (!isSuccess) {
myCk.deleteCookies(ckFilePath);
return false;
}
});
} else {
await myCk.checkToDelete(ckFilePath).then(isDel => {
if (isDel) {
return false;
}
});
}
await presenceNow.check(fs.existsSync(ckFilePath), ctx).then(data => {
for (let id = 0; id < data[0].pesan.length; id++) {
console.log(data[0].pesan[id]);
}
for (let id = 0; id < data[0].id.length; id++) {
presenceData.push(data[0].id);
}
if (data[0].pesan.length == 0 && fs.existsSync(ckFilePath)) {
myCk.deleteCookies(ckFilePath);
}
});
} catch (e) {
console.log(e);
}
return presenceData;
}
Can anyone explain why presenceNow.check() function is not calling if my ckFilePath does not exist? but if myCkFilePath is exist, my code run so well. And maybe anyone can show me the better code for that case? thanks.
Mixing async/await and promise chains like this is something of a code smell that the author lacked an understand of async/await. It's also something of a mixed metaphor.
If you refactor it to actually use async/await you get something like this that's a lot easier to understand.
My suspicion is that your presenceNow.check() method is not being called because the function is taking returning via one of the two return paths above it:
the file exists and myCk.checkToDelete() returns true, or
the file does not exist, and the login is unsuccessful.
const fs = require('fs/promises');
async function presence(ctx) {
var presenceData = [];
var isSuccess = false;
var ckFilePath = "./somepath/cookie.json";
let ckFilePathExists = await fs.access(ckFilePath);
if (ckFilePathExists) {
const isDel = await myCk.checkToDelete(ckFilePath);
if (isDel) {
return false;
}
} else {
const login = await menuLogin.login(ctx);
const isSuccess = login[0].status
myCk.saveCookies(login[0].cookies, ckFilePath);
if (!isSuccess) {
myCk.deleteCookies(ckFilePath);
return false;
}
}
ckFilePathExists = await fs.access(ckFilePath)
const data = await presenceNow.check(ckFilePathExists, ctx);
for (let id = 0; id < data[0].pesan.length; id++) {
console.log(data[0].pesan[id]);
}
for (let id = 0; id < data[0].id.length; id++) {
presenceData.push(data[0].id);
}
if (data[0].pesan.length == 0 && await fs.access(ckFilePath) ) {
myCk.deleteCookies(ckFilePath);
}
return presenceData;
}

Async call to Authorize.net not working as expected in node.js

Im setting an Authorize.com charge as per their API reference at https://developer.authorize.net/api/reference/index.html in Node.js.
I can place both ACH and CC transactions just fine, but Im facing a problem with the response from Authorize. Their response takes about 1 second.
After all the parameters such as CC number, expiration date, etc are filled out, I execute a function as follows (ctrl.execute(function () {}):
var ctrl = new ApiControllers.CreateTransactionController(createRequest.getJSON());
ctrl.execute(function () {
var apiResponse = ctrl.getResponse();
var response = new ApiContracts.CreateTransactionResponse(apiResponse);
if (response != null) {
if (response.getMessages().getResultCode() == ApiContracts.MessageTypeEnum.OK) {
if (response.getTransactionResponse().getMessages() != null) {
//transaction approved
AuthorizeResult.status = 1;
}
else {
//transaction rejected
if (response.getTransactionResponse().getErrors() != null) {
AuthorizeResult.status = 0;
}
}
}
else {
if (response.getTransactionResponse() != null && response.getTransactionResponse().getErrors() != null) {
AuthorizeResult.status = 0;
}
else {
AuthorizeResult.status = 0;
}
}
}
else {
AuthorizeResult.status = 0;
}
After I get a result from Authorize, I need to run this code, which Im unable to do, and if I place the code inside the function, I get an error:
SyntaxError: await is only valid in async function
at wrapSafe (internal/modules/cjs/loader.js:988:16)
at Module._compile (internal/modules/cjs/loader.js:1036:27)
sqlString = `
insert into AuthorizeBillings (memberid, datetime, authorizeid, messagecode, status, amount, billingtype, errorcode)
values (#memberid, #datetime, #authorizeid, #messagecode, #status, #amount, #billingtype, #errorcode )`
try {
const pool = await utils.poolPromise
const recordset = await pool.request()
.input('memberid', utils.sql.Int, memberid)
.....
.....
.input('errorcode', utils.sql.NVarChar, AuthorizeResult.errorcode)
.query(sqlString)
} catch (err) {
console.log(err)
}
I tried to run this function await but I had no luck. What I need is to continue the code execution AFTER this function returned a value, which I am not able to accomplish properly.
Thanks.
This probably isn't a proper answer... But maybe it will help you out. In order to use await, you have to declare the function as async. So, something like this will allow async workflow:
async function AuthorizeTrans() { // Notice async declaration
var ctrl = new ApiControllers.CreateTransactionController(createRequest.getJSON());
var AuthorizeResult = {
memberid: memberid,
.....
.....
errorcode: ""
}
const res = await ctrl.execute(); // You can now use await
var apiResponse = await res.getResponse();
....... // Rest of code...
SqlExecute(params);
}
async function SqlExecute(params) {
sqlString = `
insert into AuthorizeBillings (memberid, datetime, authorizeid, messagecode, status, amount, billingtype, errorcode)
values (#memberid, #datetime, #authorizeid, #messagecode, #status, #amount, #billingtype, #errorcode )`
try {
const pool = await utils.poolPromise
const recordset = await pool.request()
.input('memberid', utils.sql.Int, memberid)
.....
.....
.input('errorcode', utils.sql.NVarChar, AuthorizeResult.errorcode)
.query(sqlString)
} catch (err) {
console.log(err)
}
}
If you follow the logic from that syntax, you should be on the right track.

SYSTEM_ERROR: system encountered unexpected error. Function killed. Cloud functions

When I looked through logs from Firebase cloud functions, I noticed that some functions have an error in logs with a description of "SYSTEM_ERROR: system encountered unexpected error. Function killed.", Until yesterday, this behavior was not, here is one of these functions. How can I fix it?
const functions = require('firebase-functions');
const admin = require('firebase-admin');
module.exports = functions.database.ref('/cards/{cardID}/interestedUsers').onWrite(event => {
const cardID = event.params.cardID;
console.log("interestedUsers", event.data.val(), cardID);
var currentInterestedUsers = [];
currentInterestedUsers = event.data.val();
var previousInterestedUsers = [];
previousInterestedUsers = event.data.previous.val();
if (event.data.previous.val() && currentInterestedUsers) {
var isNewPendingRequest = false;
if (currentInterestedUsers.length < previousInterestedUsers.length) {
currentInterestedUsers.forEach((interestedUser, index) => {
const interestedUserVal = interestedUser.val();
const isApproved = interestedUser["isApproved"];
console.log("result", interestedUser.val(), interestedUserVal);
if (isApproved == false) {
isNewPendingRequest = true;
}
});
}
if (isNewPendingRequest == false) {
const cardRef = admin.database().ref("cards").child(cardID);
const setupIsNewPendingRequestPromise = cardRef.update({
"isNewPendingRequest": false
});
return Promise.all([setupIsNewPendingRequestPromise]);
};
return console.log("interestedUsers deleting");
};
if (event.data.val() == null) {
console.log("event.data.val() == null");
const cardRef = admin.database().ref("cards").child(cardID);
// check card
const cardCheckPromise = cardRef.once("value", function(cardCheckPromiseSnap, error){
if (error) {
return console.log("cardCheckPromise", error);
};
if (cardCheckPromiseSnap.val()) {
const checkCardID = cardCheckPromiseSnap.val()["id"];
if (checkCardID) {
console.log("checkCardID", checkCardID);
const setupIsNewPendingRequestPromise = cardRef.update({
"isNewPendingRequest": false
});
return Promise.all([setupIsNewPendingRequestPromise]);
} else {
return console.log("checkCardID == null");
}
} else {
return console.log("cardCheckPromiseSnap.val() == null");
};
});
return Promise.all([cardCheckPromise]).catch(function(cardCheckPromiseError){
console.log("cardCheckPromise error", cardCheckPromiseError.message, cardCheckPromiseError.messageId)
});
}
return console.log("just update or adding a new interested user");
});
You can try re-deploying your app.
Likely a GCF outage. There was one today and yesterday. These errors were being thrown during the outage.
Solution
Take a break and wait for the cloud to heal. Contact GCP Support if you've purchased support.

Resources