how to optimize code like reduce code with same functionality - javascript

let response = {};
var filters = {
topfeaturedandotherfields: req.body.topfeaturedandotherfields,
};
if (req.body.minprice && req.body.maxprice && req.body.brandName) {
var filters = {
$and: [
{ brandName: { $in: req.body.brandName } },
{ topfeaturedandotherfields: req.body.topfeaturedandotherfields },
{ salePrice: { $gte: req.body.minprice, $lte: req.body.maxprice } },
],
};
var result = await productService.getAllProductofhomepage(
filters,
req.body.ordername,
req.body.orderby
);
} else {
if (req.body.minprice && req.body.maxprice) {
var filters = {
$and: [
{ topfeaturedandotherfields: req.body.topfeaturedandotherfields },
{ salePrice: { $gte: req.body.minprice, $lte: req.body.maxprice } },
],
};
var result = await productService.getAllProductofhomepage(
filters,
req.body.ordername,
req.body.orderby
);
}
if (req.body.brandName) {
var filters = {
$and: [
{ brandName: { $in: req.body.brandName } },
{ topfeaturedandotherfields: req.body.topfeaturedandotherfields },
],
};
var result = await productService.getAllProductofhomepage(
filters,
req.body.ordername,
req.body.orderby
);
}
}
if (req.body.limit == true)
var result = await productService.getAllProductofhomepagewithlimit(filters);
else if (req.body.minprice || req.body.maxprice || req.body.brandName) {
} else {
var result = await productService.getAllProductofhomepage(
filters,
req.body.ordername,
req.body.orderby
);
}
if (result.length > 0) {
response = {
message: "Home page products successfully retrieved",
error: false,
data: result,
};
} else {
response = {
message: "Faild to get products",
error: true,
data: {},
};
}
res.status(200).json(response);
This code is used to filter like to see top feature and bestseller or min and max price and the brand name also in this code sort by order name which could be price or brand name or category also in ascending and descending order so now you can see this code is like if and else but I want to optimize and reduce code

You can make this query quite a lot nicer by just dynamically building the query condition instead of breaking the logic into if/else blocks:
export async function login(req: Request, res: Response): Promise<void> {
const response = {};
let filters = {
topfeaturedandotherfields: req.body.topfeaturedandotherfields,
};
if (req.body.minprice || req.body.maxprice) {
const saleCond = { };
if (req.body.minprice) {
saleCond.$gte = req.body.minprice;
}
if (req.body.maxprice) {
saleCond.$lte = req.body.maxprice;
}
filters.salePrice = saleCond
}
if (req.body.brandName) {
filters.brandName = {$in: req.body.brandName}
}
let result = [];
if (req.body.limit == true) {
result = await productService.getAllProductofhomepagewithlimit(filters)
} else {
result = await productService.getAllProductofhomepage(filters, req.body.ordername, req.body.orderby);
}
res.status(200).json({
message: result.length ? 'Home page products successfully retrieved' : 'Failed to get products',
error: result.length === 0,
data: result,
});
}
Not only is this much clearer we only removed a redundant DB call that was made in the process.

Related

nodejs filtering an array of objects where the filtering is partially done in an async function

I've read many similar questions and have tried a bunch of code. Unfortunately, I'm not getting my code to run :-(
So, the situation is as follows: In a route of a node.js server, I have to respond with a filtered array of Objects. Unfortunately, whatever I do, I always get an empty array [] back. The filter is a bit tricky in my opinion, as it consists of a string comparison AND an async call to a library function. With the console output, I can clearly see that the correct element is found, but at the same time I see that I've already received the object...
Here is some code that exemplifies my challenge:
let testArray = [
{
id: 'stringId1',
data: {
someDoc: {
moreContent: 'Some random content',
type: 'noInterest'
}
}
},
{
id: 'stringId2',
data: {
someDoc: {
moreContent: 'Some random content',
type: 'ofInterest'
}
}
},
{
id: 'stringId3',
data: {
someDoc: {
moreContent: 'Some random content',
type: 'ofInterest'
}
}
}
]
// code from a library. Can't take an influence in it.
async function booleanWhenGood(id) {
if (id in some Object) {
return { myBoolean: true };
} else {
return { myBoolean: false };
}
}
// Should return only elements with type 'ofInterest' and that the function booleanWhenGood is true
router.get('/', function(res,req) {
tryOne(testArray).then(tryOneResult =>{
console.log('tryOneResult', tryOneResult);
});
tryTwo(testArray).then(tryTwoResult => {
console.log("tryTwoResult ", tryTwoResult);
});
result = [];
for (const [idx, item] of testArray.entries() ) {
console.log(idx);
if (item.data.someDoc.type === "ofInterest") {
smt.find(item.id).then(element => {
if(element.found) {
result.push(item.id);
console.log("ID is true: ", item.id);
}
});
}
if (idx === testArray.length-1) {
// Always returns []
console.log(result);
res.send(result);
}
}
})
// A helper function I wrote that I use in the things I've tried
async function myComputeBoolean(inputId, inputBoolean) {
let result = await booleanWhenGood(inputId)
if (result.myBoolean) {
console.log("ID is true: ", inputId);
}
return (result.myBoolean && inputBoolean);
}
// A few things I've tried so far:
async function tryOne(myArray) {
let myTmpArray = []
Promise.all(myArray.filter(item => {
console.log("item ", item.id);
myComputeBoolean(item.id, item.data.someDoc.type === "ofInterest")
.then(myBResult => {
console.log("boolean result", myBResult)
if (myBResult) {
tmpjsdlf.push(item.id);
return true;
}
})
})).then(returnOfPromise => {
// Always returns [];
console.log("returnOfPromise", myTmpArray);
});
// Always returns []
return(myTmpArray);
}
async function tryTwo(myArray) {
let myTmpArray = [];
myArray.forEach(item => {
console.log("item ", item.id);
myCompuBoolean(item.id, item.data.someDoc.type === "ofInterest")
.then(myBResult => {
console.log("boolean result", myBResult)
if (myBResult) {
myTmpArray.push(item.did);
}
})
});
Promise.all(myTmpArray).then(promiseResult => {
return myTmpArray;
});
}
Asynchronous programming is really tough for me in this situation... Can you help me get it running?
I didn't inspect your attempts that closely, but I believe you are experiencing some race conditions (you print return and print the array before the promises resolve).
However you can alwayd use a regular for loop to filter iterables. Like this:
let testArray = [
{
id: 'stringId1',
data: {
someDoc: {
moreContent: 'Some random content',
type: 'noInterest'
}
}
},
{
id: 'stringId2',
data: {
someDoc: {
moreContent: 'Some random content',
type: 'ofInterest'
}
}
},
{
id: 'stringId3',
data: {
someDoc: {
moreContent: 'Some random content',
type: 'ofInterest'
}
}
}
]
async function booleanWhenGood(id) {
if (id in { 'stringId1': 1, 'stringId2': 1 }) { // mock object
return { myBoolean: true };
} else {
return { myBoolean: false };
}
}
async function main() {
let filtered = []
for (item of testArray)
if ((await booleanWhenGood(item.id)).myBoolean && item.data.someDoc.type === 'ofInterest')
filtered.push(item)
console.log('filtered :>> ', filtered);
}
main()

How to update async await function when a variable change?

genderPie()
let filter = {};
async function genderPie() {
const d = await getData();
const g = await d.reduce((a, o) => (o.GEN && a.push(o.GEN), a), []);
const gender = Object.keys(g).length;
const m = await d.reduce((a, o) => (o.GEN == 1 && a.push(o.GEN), a), []);
const male = Object.keys(m).length;
const f = await d.reduce((a, o) => (o.GEN == 2 && a.push(o.GEN), a), []);
const female = Object.keys(f).length;
var data = [{
name: 'male',
y: male,
id: 1
}, {
name: 'female',
y: female,
id: 2
}];
chart = new Highcharts.Chart({
plotOptions: {
pie: {
innerSize: '80%',
dataLabels: {
connectorWidth: 0
}
}
},
series: [{
"data": data,
type: 'pie',
animation: false,
point: {
events: {
click: function(event) {
filter.GEN = '' + this.id + '';
}
}
}
}],
"chart": {
"renderTo": "gender"
},
});
}
async function getData() {
buildFilter = (filter) => {
let query = {};
for (let keys in filter) {
if (filter[keys].constructor === Array && filter[keys].length > 0) {
query[keys] = filter[keys];
}
}
return query;
}
//FILTER DATA
//Returns the filtered data
filterData = (dataset, query) => {
const filteredData = dataset.filter((item) => {
for (let key in query) {
if (item[key] === undefined || !query[key].includes(item[key])) {
return false;
}
}
return true;
});
return filteredData;
};
//FETCH JSON
const dataset = [{
"GEN": "2"
}, {
"GEN": "1"
}, {
"GEN": "1"
}, {
"GEN": "2"
},
{
"GEN": "2"
}, {
"GEN": "2"
}, {
"GEN": "2"
}, {
"GEN": "1"
}
]
//BUILD THE FILTER
const query = buildFilter(filter);
const result = filterData(dataset, query);
console.log(result)
return result
}
<script src="https://code.highcharts.com/highcharts.js"></script>
<div id="gender"></div>
does anyone can explain me how to handle the following?
I have two functions that filter data and than I build a chart with Hichart
Each time a user click for example a slice of a pie chart an event is fired and an object is populated.
That object allows me to filter the dataset and redraw the chart
The last thing I'm missing is about to update the filtering functions based on the object to be populated
first I'll do this
async function getData() {
buildFilter = (filter) => {
let query = {};
for (let keys in filter) {
if (filter[keys].constructor === Array && filter[keys].length > 0) {
query[keys] = filter[keys];
}
}
return query;
}
then
filterData = (data, query) => {
const filteredData = data.filter( (item) => {
for (let key in query) {
if (item[key] === undefined || !query[key].includes(item[key])) {
return false;
}
}
return true;
});
return filteredData;
};
const query = buildFilter(filter);
const result = filterData(data, query);
my object is
let filter = {}
when a user click the slice myobject become for example
let filter = {
gen: "1"
}
Take a look at this StackBlitz project.
In getData(), I simplified your filter to this one:
return data.filter(item => {
for (const property of Object.keys(filter)) {
if (item[property] !== filter[property]) {
return false;
}
}
return true;
});
and when a slice is clicked, I call genderPie() again, after updating the filter.
You might want to separate the data request from the filtering, so that the data is downloaded only once, not every time a filter is changed.

Async-Await function returns undefined

I'm finding myself with some inconvenient to return a certain result inside an async function which has an await request inside. I tried both 3 libraries ("http", "request", "then-request"), and it's always the same.
The main idea is that when the ajax ends, its result should be returned; but instead, it returns undefined (it doesn't respect the async/await).
File: index.server.js
const PyService = require("../../../api/services/PyService/validacionCSV");
module.exports = {
pasajeClientes: async function (matcheos) {
let resultado = await PyService.validate(matcheos);
return resultado;
}
}
File: validacionCSV.js
const request = require('then-request');
module.exports = {
validate: async (matcheos) => {
var response;
await request("GET", `${process.env.API_URL}/validate`, {
json: {
csv: {
clients: "datosPersonas.csv",
products: "movimientos.csv"
},
primary_keys: {
clients: "ID",
products: "ID",
},
branches: {
products: "rama",
},
rules: {
clients: matcheos["clientes"],
products: matcheos["productos"],
}
}
}).done((resultado) => {
let matched = resultado.ok;
let no_relationships = resultado.no_relationships;
let repeated = resultado.repeated;
let total = resultado.total;
let type_errors = resultado.type_errors;
response = {
error: false,
message: "",
errorConTipoDatoClientes: type_errors.clients,
errorConTipoDatoProductos: type_errors.products,
errorConClientesSinProductos: no_relationships.clients,
errorConProductosSinCliente: no_relationships.productos,
errorConClientesRepetidos: repeated.clients,
errorConProductosRepetidos: repeated.products,
cantClientesOk: matched.clients,
cantProductosOk: matched.products,
cantClientesEnArchivo: total.clients,
cantProductosEnArchivo: total.products,
}
if (no_relationships.clients > 0 || no_relationships.products > 0
|| repeated.clients > 0 || repeated.products > 0
|| type_errors.clients > 0 || type_errors.products > 0
) {
response.error = true;
response.message = "Los clientes/productos importados poseen errores."
}
else
response.message = "Los clientes/productos importados no poseen errores."
});
return response;
}
}
You are mixing Promise callbacks with async/await. When working with callbacks you can't define a variable outside and then instantiate within the callback and then try to use it outside the call back again. Read more on Promises.
All I did was return response within the callback function.
Try this
const request = require('then-request');
module.exports = {
validate: async(matcheos) => {
var response;
await request("GET", `${process.env.API_URL}/validate`, {
json: {
csv: {
clients: "datosPersonas.csv",
products: "movimientos.csv"
},
primary_keys: {
clients: "ID",
products: "ID",
},
branches: {
products: "rama",
},
rules: {
clients: matcheos["clientes"],
products: matcheos["productos"],
}
}
}).done((resultado) => {
let matched = resultado.ok;
let no_relationships = resultado.no_relationships;
let repeated = resultado.repeated;
let total = resultado.total;
let type_errors = resultado.type_errors;
response = {
error: false,
message: "",
errorConTipoDatoClientes: type_errors.clients,
errorConTipoDatoProductos: type_errors.products,
errorConClientesSinProductos: no_relationships.clients,
errorConProductosSinCliente: no_relationships.productos,
errorConClientesRepetidos: repeated.clients,
errorConProductosRepetidos: repeated.products,
cantClientesOk: matched.clients,
cantProductosOk: matched.products,
cantClientesEnArchivo: total.clients,
cantProductosEnArchivo: total.products,
}
if (no_relationships.clients > 0 || no_relationships.products > 0 ||
repeated.clients > 0 || repeated.products > 0 ||
type_errors.clients > 0 || type_errors.products > 0
) {
response.error = true;
response.message = "Los clientes/productos importados poseen errores."
} else
response.message = "Los clientes/productos importados no poseen errores."
return response
});
}
}

GraphQL + Mongoose: How to change property of mongoose retrieved data

I'm writing a GraphQL query that will return mongoose data:
export const getInventory = () => {
let query = {
deletedAt: null
};
let stocks = await StockModel.find(query)
.sort({ material: 1 });
// Now separate each inventory item per material
let inventory = {};
stocks.map(stock => {
if (inventory[stock.material]) {
inventory[stock.material].quantity += stock.quantity;
} else {
let clone = Object.assign({}, stock);
clone.lot = null;
clone.expirationDateTime = null;
clone.partNumber = null;
clone.manufacturer = null;
inventory[stock.material] = clone;
}
});
let list = Object.keys(inventory).map(key => {
return inventory[key];
});
return list;
};
Here is my inventory GraphQL query:
const inventory = {
type: new GraphQLList(StockType),
description: "Get all inventory",
resolve(root, args, context) {
return getInventory();
}
};
When running the following query:
{
viewer {
inventory() {
id
quantity
lot
}
}
}
I'm getting the following GraphQL error:
{
"errors": [
{
"message": "Expected value of type \"Stock\" but got: [object Object].",
"locations": [
{
"line": 3,
"column": 5
}
],
"stack": "Expected value of type \"Stock\" but got: [object Object].\n\nGraphQL request (3:5)\n2: viewer {\n3: inventory {\n ^\n4: id\n\n at invalidReturnTypeError (/dev/node_modules/graphql/execution/execute.js:766:10)\n at completeObjectValue (/dev/node_modules/graphql/execution/execute.js:758:13)\n at completeValue (/dev/node_modules/graphql/execution/execute.js:660:12)\n at completeValueWithLocatedError (/dev/node_modules/graphql/execution/execute.js:580:21)\n at completeValueCatchingError (/dev/node_modules/graphql/execution/execute.js:556:21)\n at /dev/node_modules/graphql/execution/execute.js:684:25\n at Array.forEach (<anonymous>)\n at forEach (/dev/node_modules/iterall/index.js:83:25)\n at completeListValue (/dev/node_modules/graphql/execution/execute.js:680:24)\n at completeValue (/dev/node_modules/graphql/execution/execute.js:643:12)\n at /dev/node_modules/graphql/execution/execute.js:617:14\n at process._tickCallback (internal/process/next_tick.js:68:7)",
"path": [
"viewer",
"inventory",
0
]
}
}
How can I solve the error?

How to check if boolean is passed as string?

So in below code if i pass ancillaryProductInd as boolean code works, but when I pass it as a string, it does not work. In my understanding the below code should only work when I pass "false" string value and throw error on boolean. Any idea what is the issue here ?
main.ts
request
var rxInfos = [{
"ancillaryProductInd": "false",
"indexID": "eyJrZXkiOiIEOHdpNUpNWmR3PT0ifQ=="
}]
function subQuestionsHandler(rxInfos, data) {
const subQuestionArray = [];
rxInfos.forEach((rxInfo) => {
const subQuestion = {
question: []
};
if (rxInfo.ancillaryProductInd !== undefined && rxInfo.ancillaryProductInd === "false") {
subQuestion.question = data;
subQuestionArray.push(subQuestion);
}
});
return subQuestionArray;
}
subQuestionsHandler(rxInfos, [{
some data
}]);
Your example code works as expected with a string value "false" and doesnt run the if block when a boolean is used. See my example:
var rxInfos = [
{
ancillaryProductInd: "false",
indexID: "eyJrZXkiOiIEOHdpNUpNWmR3PT0ifQ=="
},
{
ancillaryProductInd: false,
indexID: "eyJrZXkiOiIEOHdpNUpNWmR3PT0ifQ=="
}
];
function subQuestionsHandler(rxInfos, data) {
const subQuestionArray = [];
rxInfos.forEach(rxInfo => {
const subQuestion = {
question: []
};
if (
rxInfo.ancillaryProductInd !== undefined &&
rxInfo.ancillaryProductInd === "false"
) {
console.log("no error");
subQuestion.question = data;
subQuestionArray.push(subQuestion);
} else {
console.log("throw error");
}
});
return subQuestionArray;
}
subQuestionsHandler(rxInfos, [
{
test: ""
}
]);

Categories