Nodejs Array is always empty, while it should be filled - javascript

const generatePropertyAmenities = async (property) => {
let result = [];
property.amenities.map(async (res, key) => {
/**
* Get the amenities type id, creates it if it doesn't exist
* const type_existed_before: defines if the type already existed
*/
let type_id = null;
const type_existed_before = await strapi
.query("amenities-categories")
.findOne({ name: res.type.trim() });
if (type_existed_before) type_id = type_existed_before.id;
else
type_id = await strapi
.query("amenities-categories")
.create({ name: res.type }).id;
res.items.map(async (amenity) => {
const amenity_exists = await strapi
.query("amenities")
.findOne({ name: amenity.name.trim() });
if (amenity_exists) {
// console.log(amenity_exists)
if (
!type_existed_before ||
(type_existed_before &&
!amenity_exists.amenities_categories.find((x) => x === type_id))
) {
await strapi.query("amenities").update(
{ id: amenity_exists.id },
{
amenities_categories: [
...amenity_exists.amenities_categories,
type_id,
],
}
);
}
result = [...result, amenity_exists.id];
} else {
result = [
...result,
await strapi
.query("amenities")
.create({
name: amenity.name.trim(),
amenities_categories: [type_id],
}).id,
];
}
});
});
console.log(result);
return result;
};
result is always empty, am I missing something here ? I really don't see what I'm doing wrong. I checked everything, amenity_exists.id is correct, everything seems correct but something is not I really need some help here guys

Ok so I managed to make it work, I don't think I can explain why and how exactly it works I may need some explanations from you guys, but here's my fix:
const generatePropertyAmenities = async (property) => {
const getAmenityCategoryId = (item) =>
new Promise(async (resolve, reject) => {
const type_existed_before = await strapi
.query("amenities-categories")
.findOne({ name: item.type.trim() });
if (type_existed_before)
resolve({
type_id: type_existed_before.id,
type_existed_before: false,
});
else
resolve({
type_id: await strapi
.query("amenities-categories")
.create({ name: item.type }).id,
type_existed_before: false,
});
});
const getAmenity = (amenity, type_id, type_existed_before) =>
new Promise(async (resolve, reject) => {
const amenity_exists = await strapi
.query("amenities")
.findOne({ name: amenity.name.trim() });
if (amenity_exists) {
// console.log(amenity_exists)
if (
!type_existed_before ||
(type_existed_before &&
!amenity_exists.amenities_categories.find((x) => x === type_id))
) {
await strapi.query("amenities").update(
{ id: amenity_exists.id },
{
amenities_categories: [
...amenity_exists.amenities_categories,
type_id,
],
}
);
}
resolve(amenity_exists.id);
} else {
resolve(
await strapi.query("amenities").create({
name: amenity.name.trim(),
amenities_categories: [type_id],
}).id
);
}
});
const createArrayOfAmenities = () =>
new Promise(async (resolve, reject) => {
let ret = [];
for (let index = 0; index < property.amenities.length; index++) {
const element = property.amenities[index];
let { type_id, type_existed_before } = await getAmenityCategoryId(
element
);
for (let k = 0; k < element.items.length; k++) {
const amenity = element.items[k];
ret = [
...ret,
await getAmenity(amenity, type_id, type_existed_before),
];
}
}
resolve(ret)
});
return await createArrayOfAmenities();
};

Try
await res.items.map(async ...

Related

Result won't update VAR

I am trying to run a query, inside AXIOS which gets data from a 3rd party URL. Then uses some of that data to search our mongoDB database.
However it seems it won't update var total = 0
While the query below does function correctly, the return result won't allow me to set that it to the query.
Promise.all(arr.forEach( async (id,index) => {
//(CODE REMOVED JUST TO GET THIS FUNCTION TO WORK)
const search = await geoLocation.find({
'location': {
'$geoWithin': {
'$box': [
[-35.2418503, -13.5076852], [112.8656697, 129.0020486]
]
}
}}).toArray();
total = search.length;
}));
See the full code below
var array = [];
var pointarray = []
var total = 0;
areas.forEach((id,index) => {
if(id.type == "Point"){
pointarray[index] = "N"+id.id;
}else{
array[index] = "R"+id.id;
}
});
var arraySearch = "https://nominatim.openstreetmap.org/lookup?osm_ids="+array.toString()+"&polygon_geojson=1&bbox=1&format=json";
var pointSearch = "https://nominatim.openstreetmap.org/lookup?osm_ids="+pointarray.toString()+"&polygon_geojson=1&bbox=0&format=json"
const requestOne = axios.get(arraySearch);
const requestTwo = axios.get(pointSearch);
axios.all([requestOne, requestTwo])
.then(axios.spread((...responses) => {
const responseOne = responses[0]
const responseTwo = responses[1]
/*
process the responses and return in an array accordingly.
*/
return [
responseOne.data,
responseTwo.data,
];
}))
.then(arr => {
Promise.all(arr.forEach( async (id,index) => {
//const middleIndex = id[index].boundingbox.length / 2;
//const firstHalf = id[index].boundingbox.splice(0, middleIndex);
//const secondHalf = id[index].boundingbox.splice(-middleIndex);
//res.send(secondHalf[0]);
const query = [{
$match: {
location: {
$geoWithin: {$box:[[Number(firstHalf[0]),Number(firstHalf[1])],[Number(secondHalf[0]),Number(secondHalf[1])]]
}
}
}
},{
$count: 'id'
}]
const search = await geoLocation.find({
'location': {
'$geoWithin': {
'$box': [
[-35.2418503, -13.5076852], [112.8656697, 129.0020486]
]
}
}}).toArray();
total = search.length;
// total = search.length;
// const search = geoLocation.aggregate(query).toArray.length;
}));
})
.catch(errors => {
console.log("ERRORS", errors);
})
.then(function () {
res.send(total);
});

How to refactor for-loop async/await with Promise.all()?

I'm trying to wrap my head around how to use Promise.all() in this code. I've read on articles that you can run async operations in parallel with Promise.all() to optimize for speed. Here's the current code in nested for-loops (bad):
type ListGroup = {
listId: string
groupIds: Array<string>
}
const listsAndGroups: Array<ListGroup> = []; // <-- put everything here
const { lists } = await mailchimp.get('/lists');
for (const list of lists) {
const listObj = { listId: list.id };
const { categories } = await mailchimp.get(
`/lists/${list.id}/interest-categories`,
);
for (const category of categories) {
const { interests } = await mailchimp.get(
`/lists/${list.id}/interest-categories/${category.id}/interests`,
);
Object.defineProperty(listObj, 'groupIds', {
value: interests.map((interest) => interest.id),
enumerable: true,
});
}
listsAndGroups.push(listObj);
}
Here's how I'm doing so far, I think I'm just running blindly here without really knowing what I'm doing:
const listsAndGroups: Array<ListGroup> = await getListsGroups(); // <-- put everything here
const getListsGroups = async () => {
const { lists } = await mailchimp.get('/lists');
const listGroups = lists.map((list) =>
getCategories(list.id).then((groups) =>
groups.map((group: Record<'groupIds', string>) => {
return {
listId: list.id,
...group,
};
}),
),
);
return Promise.all(listGroups);
};
const getCategories = async (listId: string) => {
const { categories } = await mailchimp.get(
`/lists/${listId}/interest-categories`,
);
const groups = categories.map((category) =>
getInterests(listId, category.id),
);
return Promise.all(groups);
};
const getInterests = async (listId: string, categoryId: string) => {
const { interests } = await mailchimp.get(
`/lists/${listId}/interest-categories/${categoryId}/interests`,
);
return { groupIds: interests.map((interest) => interest.id) };
};
You could simplify your operation many way, Here is one:
type ListGroup = {
listId: string
groupIds: Array<string>
}
const listsAndGroups: Array<ListGroup> = []; // <-- put everything here
const { lists } = await mailchimp.get('/lists');
const pandingLists = lists.map(list =>
mailchimp.get(`/lists/${list.id}/interest-categories`)
.then(data => [data, { listId: list.id }])
);
for (const [{ categories }, listObj] of await Promise.all(pandingLists)) {
const batch = categories.map(({ id }) =>
mailchimp.get(`/lists/${listObj.listId}/interest-categories/${id}/interests`).then(interests => {
Object.defineProperty(listObj, 'groupIds', {
value: interests.map(({ id }) => id),
enumerable: true,
});
}));
await Promise.all(batch).then(() => listsAndGroups.push(listObj));
}

Asynchronous indexedDB in a class object

I have written a tiny indexedDB library but dosen't seem to work as expected. So my question is how do i fix or properly reconstruct the code bellow to work as expected with a promise. I'm out of idea.
Although the add and delete method works in terms of successfully adding and deleting item from the database but the delete method won't signal it .then function
class IDB {
constructor(name, version) {
this.name = name;
this.version = version;
this.db = null;
}
open(callback) {
const req = window.indexedDB.open(this.name, this.version);
req.onupgradeneeded = (e) => {
let db = e.target.result;
callback(db);
}
req.onsuccess = (e) => {
this.db = e.target.result;
this.db.onversionchange = () => {
this.db.close();
console.log('Tell user to reload the page');
}
}
req.onerror = (e) => {
let db = e.target.errorCode;
return;
}
}
result(req) {
return new Promise((resolve, reject) => {
if (req) {
req.onsuccess = (e) => {
let res = e.target.result;
if (res) {
resolve(res);
}
};
req.onerror = (e) => {
let err = e.target.errorCode;
if (err) {
reject(err);
}
};
}
})
}
add(store, Items) {
let req;
let tx = this.db.transaction([store], 'readwrite').objectStore(store);
Items.forEach(item => {
req = tx.add(item);
});
return this.result(req);
}
get(store, key) {
let req = this.db.transaction([store], 'readonly')
.objectStore(store).get(key);
return this.result(req);
}
cursor(store) {
let req = this.db.transaction(store).objectStore(store)
.openCursor();
return this.result(req);
}
delete(store, key) {
let req = this.db.transaction([store], 'readwrite')
.objectStore(store).delete(key);
return this.result(req);
}
}
Usage
const employeeData = [
{ id: "00-01", name: "gopal", age: 35, email: "gopal#tutorialspoint.com" },
{ id: "00-02", name: "prasad", age: 32, email: "prasad#tutorialspoint.com" }
];
const idb = new IDB('mydb', 1);
idb.open((db) => {
if (!db.objectStoreNames.contains('user')) {
db.createObjectStore('user', {keyPath: 'id'});
}
});
idb.add('user', employeeData).then(() => alert('Items added')).catch(() => alert('failed'));
But I would prefer the usage with the async/await code
await idb.add('..', ..) // blah blah

I am trying to resolve the data in map function

Below is my code.I am unable resolve the o/p.How to resolve in async and await.I am trying to get data from other collection and pass it to existing filtered collection
exports.listStatus = async (root, { _id, start, limit }) => {
var user = await db.User.find({ _id: _id })
let list = user[0].savedUsers
var blocked = user[0].blockedUsers
var listAll = list.push(_id)
let listOfStatus = await db.Status.find({ userId: { $in: list }, reportStatus: { $ne: _id }, _id: { $nin: blocked } });
let array;
let leng = listOfStatus.length
let statusCommentsArray
var arrayStatus = [];
listOfStatus.forEach((status, key) => {
let promise = new Promise((resolve, reject) => {
var obj = {}
var statusId = status._id
var listOfLiked = status.like
let likeCount = listOfLiked.length
var sortedList = listOfLiked.sort({ createdDate: -1 })
obj.likeCount = likeCount
db.Comment.find({ statusId: statusId }, (err, statusCommentsList) => {
var statusCommentsCount = statusCommentsList.length
var sortedList = statusCommentsList.sort({ createdDate: -1 })
if (statusCommentsCount >= 3) {
statusCommentsArray = sortedList
}
else {
statusCommentsArray = sortedList.slice(0, 3)
}
obj.statusCommentsCount = statusCommentsCount
obj.statusCommentsArray = statusCommentsArray
arrayStatus.push(obj)
leng--;
if (leng == 0) {
resolve(arrayStatus)
}
})
})
promise.then((response) => {
return Promise.resolve(response)
})
})
}

Async-await 'Promise {<pending>} with Array.prototype.map

I know there are many questions discuss about the same error and I saw most of them and they didn't fix my problem.
I wrote this code:
const userOrganizationGroups = (organizationGroupsList) => {
if (Array.isArray(organizationGroupsList) && organizationGroupsList.length) {
const result = organizationGroupsList.map(async (element) => {
const { organizationId, groupId } = element;
const { Organizations, Groups } = models;
const organization = await Organizations.findOne(
{ _id: organizationId },
{ name: 1, _id: 0 },
);
const group = await Groups.findOne({ _id: groupId });
return Object.assign({}, {
organizationName: organization.name,
group: group.name,
});
});
return result;
}
};
when I debug the code:
console.log('userOrganizationGroups : ',userOrganizationGroups(list))
I got such a result:
userOrganizationGroups: Promise { <pending> }
I found a similair question: Promise { } - Trying to await for .map and I used the solution mentioned in the question:
const userOrganizationGroups = async (organizationGroupsList) => {
if (Array.isArray(organizationGroupsList) && organizationGroupsList.length) {
const result = await Promise.all(organizationGroupsList.map(async (element) => {
const { organizationId, groupId } = element;
const { Organizations, Groups } = models;
const organization = await Organizations.findOne(
{ _id: organizationId },
{ name: 1, _id: 0 },
);
const group = await Groups.findOne({ _id: groupId });
return Object.assign({}, {
organizationName: organization.name,
group: group.name,
});
}));
return result;
}
How can I fix this issue?
instead of
console.log('userOrganizationGroups : ',userOrganizationGroups(list))
use
userOrganizationGroups(list).then( groups => console.log('userOrganizationGroups : ', groups)
or
(async () => {
const groups = await userOrganizationGroups(list);
console.log('userOrganizationGroups : ', groups);
})();
console.log() was called first because you didn't wait using await or then.
So You should write below instead of
console.log('userOrganizationGroups : ',userOrganizationGroups(list))
;(async () => {
const resultGroups = await userOrganizationGroups(list)
resultGroups.forEach(group => {
console.log(`group: ${JSON.stringfy(group, null, ' ')}`)
})
})()

Categories

Resources