Nodejs array push is empty when placed after await - node.js

I am trying to loop through an array, get the quantity and prices of each stock from the database, do some calculations and push them to an array
But after getting the quantity and prices from the database and push to the array
The array will still be empty
If I remove this line
const db_stock = await Stocks.findById(stock.stockId);
everything works fine
But if I add the await back the array becomes empty
import mongoose from "mongoose";
import { response } from "express";
import StockDispatches from "../models/StockDispatch.js"
import Stocks from "../models/Stocks.js"
import { createError } from "../error.js";
import validator from 'express-validator'
const { validationResult } = validator
import { generateRamdom } from "../utils/Utils.js"
export const createStockDispatched = async (req, res, next) => {
const error = validationResult(req).formatWith(({ msg }) => msg);
const trx_id = generateRamdom(30);
let quantity = 0;
let total = 0;
let dispatchedTotal = 0;
const hasError = !error.isEmpty();
if (hasError) {
res.status(422).json({ error: error.array() });
} else {
const options={ordered: true};
let user_stocks =[];
req.body.stocks.map(async (stock, index) => {
let total = stock.price * stock.quantity
const db_stock = await Stocks.findById(stock.stockId);
if(!db_stock) return res.status(404).json({msg: "Stock Not Found."})
if( stock.quantity > db_stock.quantity)
return res.status(208).json({msg: `Quantity of ${stock.name} is greater than what we have in database`})
quantity = db_stock.quantity - stock.quantity;
total = quantity * db_stock.price;
const updated_stock = await Stocks.findByIdAndUpdate(stock.id, {quantity, total},{$new: true})
dispatchedTotal = stock.quantity * db_stock.price;
user_stocks.push("samson")
user_stocks.push({...stock, staffId: req.user.id, total: dispatchedTotal, trx_id, stockId: stock.id, price: db_stock.price})
});
try{
const stockDispatched = await StockDispatches.insertMany(user_stocks, options);
if(!stockDispatched) return res.status(500).json({msg: "Error. Please try again."})
return res.status(200).json({msg: "Stock uploaded successfully.."})
}catch(error){
next(error)
}
}
}

The simplest way to fix this is to change
req.body.stocks.map()
to:
for (let [index, stock] of req.body.stocks.entries()) { ... }
since the for loop is promise-aware and will pause the loop for your await inside the loop body whereas .map() does not pause its loop for an await in the callback.

Related

How to return data in foreach loop having multiple async functions inside

I am calling this function using await and store the data in the result variable. But the function returns without completing the foreach loop. How can I make this function return only if the foreach loop ends?
let result = await prepareStocks(data);
async function prepareStocks(incomingStocks) {
var stockCodes = incomingStocks.stocks.split(',');
var stockPrices = incomingStocks.trigger_prices.split(',');
var alertName = incomingStocks.alert_name;
stockCodes.forEach(async(stocks, index) => {
if (stockPrices[index] > 100) {
var stockCodes = {
code: stocks,
price: stockPrices[index],
orderType: (urls.buy.includes(alertName) ? 'BUY' : 'WATCH'),
target: await setSellPrice(stockPrices[index], 1),
stopLoss: await setStopLoss(stockPrices[index], 1),
}
STOCKS.push(stockCodes);
}
});
return STOCKS;
}
Try using Promise.all
Something like this =>
let result = await prepareStocks(data);
async function prepareStocks(incomingStocks) {
var stockCodes = incomingStocks.stocks.split(',');
var stockPrices = incomingStocks.trigger_prices.split(',');
var alertName = incomingStocks.alert_name;
const STOCKS = await Promise.all(stockCodes.map(async (stocks, index) => {
if (stockPrices[index] > 100) {
var stockCodes = {
code: stocks,
price: stockPrices[index],
orderType: (urls.buy.includes(alertName) ? 'BUY' : 'WATCH'),
target: await setSellPrice(stockPrices[index], 1),
stopLoss: await setStopLoss(stockPrices[index], 1),
}
return stockCodes;
}
})
return STOCKS;
}
After reading the async/await part, I was wondering there is nothing wrong with the way how async/await is used & STOCKS must be returned with expected values. So, I setup a simulation as follows & ran the code.
async function setSellPrice(price) { return new Promise(function resolver(resolve) {setTimeout(function someTimeLater() { resolve(price)});}); }
async function setStopLoss(price) { return new Promise(function resolver(resolve) {setTimeout(function someTimeLater() { resolve(price)});}); }
const data = { stocks: 'A,B,C'. trigger_prices: '100,120,140', alert_name: 'XYZ' };
let result = await prepareStocks(data);
And got STOCKS not defined error.
Your code was correct in terms of how async/await works. The problem with the original code was, the STOCKS variable was not defined. I am assuming, it was not defined anywhere else. So, doing the following works as expected.
let result = await prepareStocks(data);
async function prepareStocks(incomingStocks) {
const STOCKS = [];
var stockCodes = incomingStocks.stocks.split(',');
var stockPrices = incomingStocks.trigger_prices.split(',');
var alertName = incomingStocks.alert_name;
stockCodes.forEach(async(stocks, index) => {
if (stockPrices[index] > 100) {
var stockCodes = {
code: stocks,
price: stockPrices[index],
orderType: (urls.buy.includes(alertName) ? 'BUY' : 'WATCH'),
target: await setSellPrice(stockPrices[index], 1),
stopLoss: await setStopLoss(stockPrices[index], 1),
}
STOCKS.push(stockCodes);
}
});
return STOCKS;
}

Can I use #google-cloud/logging Node.js library to getEntries filtering by date?

I've got this code to getEntries from my project's cloud-logging.
import { Logging } from "#google-cloud/logging";
const PROJECT_ID = "XXXXX";
const logging = new Logging({ projectId: PROJECT_ID });
const getAdminLogEntries = async () => {
const result = await logging.getEntries({
filter: `logName="projects/XXXXX/logs/my-custom-log-name"`,
});
const entryList = result[0];
for (const entry of entryList) {
console.log(`entry.metadata: ${JSON.stringify(entry.metadata)}`);
console.log(`entry.data: ${JSON.stringify(entry.data)}`);
console.log(`---`);
}
};
getAdminLogEntries();
But I'm only getting 6 results (the oldest one is from yesterday). And I guess it's because the query is not going too far back in time. Can it filter it by date? Ex: from 2021-01-01 to 2021-01-31?
Here is what I've found out.
Reference:
https://cloud.google.com/logging/docs/view/advanced-queries
https://cloud.google.com/logging/docs/reference/libraries#list_log_entries
I was able to filter by date with the following code:
import { Logging } from "#google-cloud/logging";
const PROJECT_ID = "XXXX";
const logging = new Logging({ projectId: PROJECT_ID });
const filterItems = [
`logName="projects/XXXXX/logs/admin-logs"`,
`timestamp >= "2021-02-01T00:00:00Z"`,
`timestamp < "2021-03-01T00:00:00Z"`,
`severity="WARNING"`,
];
// JOINING FILTERS WITH "AND" OPERATOR
const filters = filterItems.join(" AND ");
const getAdminLogEntries = async () => {
const result = await logging.getEntries({
filter: filters,
});
const entryList = result[0];
for (const entry of entryList) {
console.log(`entry.metadata.severity: ${JSON.stringify(entry.metadata.severity)}`);
console.log(`entry.metadata.timestamp: ${JSON.stringify(entry.metadata.timestamp)}`);
console.log(`entry.data.message: ${JSON.stringify(entry.data.message)}`);
console.log(`---`);
}
};
getAdminLogEntries();

async function doesn't wait of inside await in nodejs

I am implementing function monthlyRevenue.
Simply, it will return total monthly revenue,and it takes arguments of station array which will make revenues, month and year.
Problem
Inside of this function I have getStationPortion which will fetch the revenue portion of user's.
So I would like to make it return object like this.
stationsPortion = {station1 : 30, station2 : 20}
In the monthlyRevenue
const stationPortions = await getStationPortions(stations)
console.log("portion map", stationPortions //it will be shown very beginning with empty
getStationPortions
const getStationPortions = async (stations) => {
let stationPortions = {}
stations.map(async (value) => {
const doc = await fdb.collection('Stations').doc(value).get()
if (!doc.exists) {
console.log("NO DOC")
} else {
stationPortions[value] = doc.data().salesPortion
console.log(stationPortions) //it will be shown at the last.
}
})
return stationPortions
}
I thought that async function should wait for the result, but it does not.
I am kind of confusing if my understanding is wrong.
Thank you
(by the way, fdb is firebase admin(firestore)
Working code
const getStationPortions = async (stations) => {
let stationPortions = {}
await Promise.all(stations.map(async (value) => {
const doc = await fdb.collection('Stations').doc(value).get()
if (!doc.exists) {
console.log("NO DOC")
} else {
stationPortions[value] = doc.data().salesPortion
console.log(stationPortions)
}
}))
return stationPortions
}
module.exports = router;

How to convert the auth response into array of objects?

I am trying to get the response of the users using auth function and i have to create an excel sheet using the xlsx-populate library and i am able to convert that into an array of objects as the limit is 1000 so there are multiple arrays of objects. and i am not able to figure out how can i do this problem.in this problem, i am simply fetching results using auth and try to get the results into an array of objects. and i am also tried to use the objects to pass into the excel sheet but it gives the excel sheet with last 1000 queries response
const admin = require("firebase-admin");
const momentTz = require("moment-timezone");
const XlsxPopulate = require("xlsx-populate");
momentTz.suppressDeprecationWarnings = true;
const {
alphabetsArray
} = require("./constant");
var start = momentTz().subtract(4, "days").startOf("day").format();
var start = momentTz(start).valueOf();
const end = momentTz().subtract(1, "days").endOf("day").format();
const listAllUsers = async(nextPageToken) =>{
const [workbook] = await Promise.all([
XlsxPopulate.fromBlankAsync()
]);
const reportSheet = workbook.addSheet("Signup Report");
workbook.deleteSheet("Sheet1");
reportSheet.row(1).style("bold", true);
[
"Date",
"TIME",
"Phone Number"
].forEach((field, index) => {
reportSheet.cell(`${alphabetsArray[index]}1`).value(field);
});
let count = 0
// List batch of users, 1000 at a time.
const data = [];
admin
.auth()
.listUsers(1000, nextPageToken)
.then (async (listUsersResult) => {
listUsersResult.users.forEach((userRecord) =>{
const time = userRecord.metadata.creationTime;
const timestamp = momentTz(time).valueOf();
// console.log(timestamp)
if (timestamp >= 1585704530967 ) {
console.log(time);
let column = count+2;
count++;
data.push(userRecord.toJSON())
reportSheet.cell(`A${column}`).value(time);
reportSheet.cell(`C${column}`).value(userRecord.phoneNumber);
}
});
console.log(JSON.stringify(data))//this is the array of the object and i am getting after 1000 response
if (listUsersResult.pageToken) {
// List next batch of users.
listAllUsers(listUsersResult.pageToken);
await workbook.toFileAsync("./SignUp.xlsx");
}
})
// .catch(function (error) {
// console.log("Error listing users:", error);
// });
// const datas = []
// datas.push(data)
// console.log(datas)
return ;
}
// Start listing users from the beginning, 1000 at a time.
listAllUsers();
and the output i am getting is like this
[]
[]
[]
[]
[]
i want to convert this into a single array of response
You have a race condition. When you perform your console.log(JSON.stringify(data)) your listUserQuery is in progress (and in async mode) and you don't have yet the answer when you print the array. Thus the array is empty.
Try this (I'm not sure of this optimal solution, I'm not a nodeJS dev)
admin
.auth()
.listUsers(1000, nextPageToken)
.then (async (listUsersResult) => {
listUsersResult.users.forEach((userRecord) =>{
const time = userRecord.metadata.creationTime;
const timestamp = momentTz(time).valueOf();
// console.log(timestamp)
if (timestamp >= 1585704530967 ) {
console.log(time);
let column = count+2;
count++;
data.push(userRecord.toJSON())
reportSheet.cell(`A${column}`).value(time);
reportSheet.cell(`C${column}`).value(userRecord.phoneNumber);
}
}
console.log(JSON.stringify(data))//this is the array of the object and i am getting after 1000 response
if (listUsersResult.pageToken) {
// List next batch of users.
listAllUsers(listUsersResult.pageToken);
await workbook.toFileAsync("./SignUp.xlsx");
}
);

How to make Mongoose update work with await?

I'm creating a NodeJS backend where a process reads in data from a source, checks for changes compared to the current data, makes those updates to MongoDB and reports the changes made. Everything works, except I can't get the changes reported, because I can't get the Mongoose update action to await.
The returned array from this function is then displayed by a Koa server. It shows an empty array, and in the server logs, the correct values appear after the server has returned the empty response.
I've digged through Mongoose docs and Stack Overflow questions – quite a few questions about the topic – but with no success. None of the solutions provided seem to help. I've isolated the issue to this part: if I remove the Mongoose part, everything works as expected.
const parseJSON = async xmlData => {
const changes = []
const games = await Game.find({})
const gameObjects = games.map(game => {
return new GameObject(game.name, game.id, game)
})
let jsonObj = require("../sample.json")
Object.keys(jsonObj.items.item).forEach(async item => {
const game = jsonObj.items.item[item]
const gameID = game["#_objectid"]
const rating = game.stats.rating["#_value"]
if (rating === "N/A") return
const gameObject = await gameObjects.find(
game => game.bgg === parseInt(gameID)
)
if (gameObject && gameObject.rating !== parseInt(rating)) {
try {
const updated = await Game.findOneAndUpdate(
{ _id: gameObject.id },
{ rating: rating },
{ new: true }
).exec()
changes.push(
`${updated.name}: ${gameObject.rating} -> ${updated.rating}`
)
} catch (error) {
console.log(error)
}
}
})
return changes
}
Everything works – the changes are found and the database is updated, but the reported changes are returned too late, because the execution doesn't wait for Mongoose.
I've also tried this instead of findOneAndUpdate():
const updated = await Game.findOne()
.where("_id")
.in([gameObject.id])
.exec()
updated.rating = rating
await updated.save()
The same results here: everything else works, but the async doesn't.
As #Puneet Sharma mentioned, you'll have to map instead of forEach to get an array of promises, then await on the promises (using Promise.all for convenience) before returning changes that will then have been populated:
const parseJSON = async xmlData => {
const changes = []
const games = await Game.find({})
const gameObjects = games.map(game => {
return new GameObject(game.name, game.id, game)
})
const jsonObj = require("../sample.json")
const promises = Object.keys(jsonObj.items.item).map(async item => {
const game = jsonObj.items.item[item]
const gameID = game["#_objectid"]
const rating = game.stats.rating["#_value"]
if (rating === "N/A") return
const gameObject = await gameObjects.find(
game => game.bgg === parseInt(gameID)
)
if (gameObject && gameObject.rating !== parseInt(rating)) {
try {
const updated = await Game.findOneAndUpdate(
{ _id: gameObject.id },
{ rating: rating },
{ new: true }
).exec()
changes.push(
`${updated.name}: ${gameObject.rating} -> ${updated.rating}`
)
} catch (error) {
console.log(error)
}
}
})
await Promise.all(promises)
return changes
}
(The diff, for convenience:
9,10c9,10
< let jsonObj = require("../sample.json")
< Object.keys(jsonObj.items.item).forEach(async item => {
---
> const jsonObj = require("../sample.json")
> const promises = Object.keys(jsonObj.items.item).map(async item => {
33a34
> await Promise.all(promises)
)
EDIT: a further refactoring would be to use that array of promises for the change descriptions themselves. Basically changePromises is an array of Promises that resolve to a string or null (if there was no change), so a .filter with the identity function will filter out the falsy values.
This method also has the advantage that changes will be in the same order as the keys were iterated over; with the original code, there's no guarantee of order. That may or may not matter for your use case.
I also flipped the if/elses within the map function to reduce nesting; it's a matter of taste really.
Ps. That await Game.find({}) will be a problem when you have a large collection of games.
const parseJSON = async xmlData => {
const games = await Game.find({});
const gameObjects = games.map(game => new GameObject(game.name, game.id, game));
const jsonGames = require("../sample.json").items.item;
const changePromises = Object.keys(jsonGames).map(async item => {
const game = jsonGames[item];
const gameID = game["#_objectid"];
const rating = game.stats.rating["#_value"];
if (rating === "N/A") {
// Rating from data is N/A, we don't need to update anything.
return null;
}
const gameObject = await gameObjects.find(game => game.bgg === parseInt(gameID));
if (!(gameObject && gameObject.rating !== parseInt(rating))) {
// Game not found or its rating is already correct; no change.
return null;
}
try {
const updated = await Game.findOneAndUpdate(
{ _id: gameObject.id },
{ rating: rating },
{ new: true },
).exec();
return `${updated.name}: ${gameObject.rating} -> ${updated.rating}`;
} catch (error) {
console.log(error);
}
});
// Await for the change promises to resolve, then filter out the `null`s.
return (await Promise.all(changePromises)).filter(c => c);
};

Resources