NodeJS Array not filling up - node.js

Im using mongodb as a database after getting the valuation data which are more then one , i loop inside them and get the offers according to the offres_id , I asure you that the database has data inside and to querys are working correctly in fact if i console log them in each iteration i get this result
{
offre: [
{
_id: new ObjectId("63320bf87123db5691c51392"),
user_id: '63304e44aa63c519d887dac1',
brand: 'HP',
model: 'AZGGH-89855A',
photo: '16642242480661659650294043-bs-5.png',
scan_method: 'manual',
__v: 0
}
],
valuation: {
_id: new ObjectId("63320d39a5677df3cebcbdae"),
user_id: '63304e44aa63c519d887dac1',
offre_id: '63320bf87123db5691c51392',
given_price: '1236',
comment: 'no comment',
__v: 0
}
}
{
offre: [
{
_id: new ObjectId("6334202a8c7e6d90b35ee999"),
user_id: '63304e44aa63c519d887dac1',
brand: 'DELL',
model: 'AZGGH-89855A',
photo: '1664360490280Capture.PNG',
scan_method: 'manual',
__v: 0
}
],
valuation: {
_id: new ObjectId("633420be8c7e6d90b35ee99e"),
user_id: '63304e44aa63c519d887dac1',
offre_id: '6334202a8c7e6d90b35ee999',
__v: 0
}
}
but when i try storing each offre & valuation at the same array cell and return it i get this as a result
[]
However this is the code
router.get('/get', async (req, res) => {
try {
Valuation.find({ user_id: req.session.userID })
.exec()
.then(valuation => {
let myData = [];
if (valuation) {
for (let i = 0; i < valuation.length; i++) {
Offre.find({_id : valuation[i].offre_id})
.exec()
.then(offre=>{
myData.push({offre : offre, valuation : valuation[i]})
})
}
res.status(200).json(myData)
} else {
res.status(404).json('no valuations found')
}
})
.catch(error => {
res.status(500).json(error.message)
})
} catch (error) {
res.status(500).json({ error: error.message })
}
})

Since you're already using async, it would be a shame to not use await to simplify your code:
router.get("/get", async (req, res) => {
try {
const valuation = await Valuation.find({ user_id: req.session.userID }).exec();
if (valuation) {
let myData = [];
for (let i = 0; i < valuation.length; i++) {
const offre = await Offre.find({ _id: valuation[i].offre_id }).exec();
myData.push({ offre: offre, valuation: valuation[i] });
}
res.status(200).json(myData);
} else {
res.status(404).json("no valuations found");
}
} catch (error) {
res.status(500).json({ error: error.message });
}
});
You can probably also speed up the Offre query by using an $in query to retrieve all offres with one query, but that's another thing.

Related

Submitting without files resets the all images in the array when making a PATCH request

I'm trying to make a dynamic field for adding team members using Formik.
In my backend, if I do not choose any file and edit only other field such as "memberName" I'm getting message saying;
"Cast to embedded failed for value "{
_id: '63c5687832a80d5d8f717715',
memberName: 'qqaa',
memberJobTitle: 'qq',
memberDescription: 'qq',
images: [ 'undefined' ]
}" (type Object) at path "team" because of "CastError""
I want to keep the existing images if there is no changes in the input field. I'm having this issue for a week and couldn't figure it out.
This is my controller for making a PATCH request;
const updateSection = async (req, res, next) => {
const files = req.files;
const {
pageId,
welcomeTitle,
welcomeDescription,
aboutUsTitle,
aboutUsDescription,
team,
teamTitle,
} = req.body;
let updates = {};
//update other fields if they are provided in the request body
if (welcomeTitle) {
updates.welcomeTitle = welcomeTitle;
}
if (welcomeDescription) {
updates.welcomeDescription = welcomeDescription;
}
if (aboutUsTitle) {
updates.aboutUsTitle = aboutUsTitle;
}
if (aboutUsDescription) {
updates.aboutUsDescription = aboutUsDescription;
}
if (teamTitle) {
updates.teamTitle = teamTitle;
}
if (team) {
let teamPromises = []; //create an empty array to store promises for updating or creating team members
// updates.team.images = [];
team.forEach((item, i) => {
// item -> current team member being processed, i-> index in the array
let teamMember = {
_id: item._id,
memberName: item.memberName,
memberJobTitle: item.memberJobTitle,
memberDescription: item.memberDescription,
images: item.images,
};
if (files && files[i]) {
let file = files[i];
let img = fs.readFileSync(file.path);
let decode_image = img.toString("base64");
teamMember.images = [
{
filename: file.originalname,
contentType: file.mimetype,
imageBase64: decode_image,
},
];
} else {
teamMember.images = item.images;
}
teamPromises.push(
Section.updateOne(
{ pageId: pageId, "team._id": item._id },
{ $set: { "team.$": teamMember } },
{ new: false }
)
);
});
Promise.all(teamPromises)
.then((result) => {
res.status(200).json({ message: "Section updated successfully!" });
})
.catch((error) => {
res.status(500).json({ error: error.message });
});
} else {
//update other fields if no team member provided
Section.findOneAndUpdate({ pageId: pageId }, updates).then(() => {
res.status(200).json({ message: "Section updated successfully!" });
});
}
};

How to remove multiple records in nodejs using mongoose

Tried to remove multiple records in single call from mongodb using mongoose but not working.Where i want to change in my code.Please help to find solution.
In my code if i use like this.. it is working..
({ p_id: { $in: ['Cs1', 'Cs2', 'Cs3']} }
but if use below like
({ p_id: { $in: [records_pids] } } it is not working.Because i am getting this array values by api call.
MongoDB:
{
p_id:"Cs1",
name:"Test",
value:"Power"
},
{
p_id:"Cs2",
name:"Test",
value:"Power"
},
{
p_id:"Cs3",
name:"Test",
value:"Power"
},
{
p_id:"Cs4",
name:"Test",
value:"Power"
},
{
p_id:"Cs5",
name:"Test",
value:"Power"
}
data.controller.js:
module.exports.deleteMultipleRecord = (req, res, next) => {
var collectionMDName = req.query.collectionname;
var records_pids = req.query.pids; //Array value Cs1, Cs2, Cs3
var tableMDModal = mongoose.model(collectionMDName);
tableMDModal.deleteMany({ p_id: { $in: [records_pids] } }, function(err, docs) {
if (err) {
console.log('ss' + err);
return
} else {
console.log("Successful deleted selected records");
res.json({ data: docs, success: true, msg: 'Successful deleted selected records.', cname: collectionMDName });
}
})
}
module.exports.deleteMultipleRecord = (req, res, next) => {
var collectionMDName = req.query.collectionname;
var records_pids = req.query.pids; //Array value CS1, CS2, CS3
var tableMDModal = mongoose.model(collectionMDName);
tableMDModal.deleteMany({ p_id: { $in: records_pids } }, function(err, docs) {
if (err) {
console.log('ss' + err);
return
} else {
console.log("Successful deleted selected records");
res.json({ data: docs, success: true, msg: 'Successful deleted selected records.', cname: collectionMDName });
}
})
}
the error is semantic , rather than searching for values $in: [CS1, CS2, CS3], the search is being made as [[CS1, CS2, CS3]]
Also,have a look at https://mongoosejs.com/docs/models.html for defining models.
MongoDB Enterprise Cluster0-shard-0:PRIMARY> use new
switched to db new
MongoDB Enterprise Cluster0-shard-0:PRIMARY> use neo
switched to db neo
MongoDB Enterprise Cluster0-shard-0:PRIMARY> db.coll.insertMany([{ p_id:"Cs1", name:"Test", value:"Power" }, { p_id:"Cs2", name:"Test", value:"Power" }, { p_id:"Cs3", name:"Test", value:"Power" }, { p_id:"Cs4", name:"Test", value:"Power" }, { p_id:"Cs5", name:"Test", value:"Power" }])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5ecb90001a40be1d77da2aa8"),
ObjectId("5ecb90001a40be1d77da2aa9"),
ObjectId("5ecb90001a40be1d77da2aaa"),
ObjectId("5ecb90001a40be1d77da2aab"),
ObjectId("5ecb90001a40be1d77da2aac")
]
}
MongoDB Enterprise Cluster0-shard-0:PRIMARY> const records_id =["Cs1","Cs2","Cs3"]
MongoDB Enterprise Cluster0-shard-0:PRIMARY> db.coll.deleteMany({p_id:{$in:records_id}})
{ "acknowledged" : true, "deletedCount" : 3 }

Import large amounts of data, but do a .find() for each element

I have a collection of customers of 60.000 items. I need to import a list of people, of 50.000. But for each person, I need to find the ID of the customer and add that to the object that is being inserted.
How should this be done most efficently?
export default async ({ file, user, database }: Request, res: Response) => {
try {
const csv = file.buffer.toString("utf8");
let lines = await CSV({
delimiter: "auto" // delimiter used for separating columns.
}).fromString(csv);
let count = {
noCustomer: 0,
fails: 0
};
let data: any = [];
await Promise.all(
lines.map(async (item, index) => {
try {
// Find customer
const customer = await database
.model("Customer")
.findOne({
$or: [
{ "custom.pipeID": item["Organisasjon - ID"] },
{ name: item["Person - Organisasjon"] }
]
})
.select("_id")
.lean();
// If found a customer
if (!isNil(customer?._id)) {
return data.push({
name: item["Person - Navn"],
email: item["Person - Email"],
phone: item["Person - Telefon"],
customer: customer._id,
updatedAt: item["Person - Oppdater tid"],
createdAt: item["Person - Person opprettet"],
creator: users[item["Person - Eier"]] || "5e4bca71a31da7262c3707c5"
});
}
else {
return count.noCustomer++;
}
} catch (err) {
count.fails++;
return;
}
})
);
const people = await database.model("Person").insertMany(data)
res.send("Thanks!")
} catch (err) {
console.log(err)
throw err;
}
};
My code just never sends an response If I use this as a Express request.

async await with promise all

I want to know if I am using promise.all correctly with async await.
Basically, I need to get the house data based on the ID, then I need to get all the reviews for that house as well as the count of reviews.
server.get("/api/houses/:id", async (req, res) => {
const { id } = req.params;
const house = await House.findByPk(id);
if (!house) {
return res.status(400).send("No house found");
}
const reviews = await Review.findAndCountAll({
where: {
houseId: house.id
}
});
house.dataValues.reviewsCount = reviews.count;
const results = await Promise.all([house.dataValues, reviews.rows]);
console.log(results);
res.send(results);
});
In the front end when I console.log the response after making the http request, I get back the below which seems okay since Promise.all gives you arrays. But I don't know if this is the best way to do this or if there is a better way.
[
{
id: 2329,
host: 2,
picture: '/img/houses/1.jpg',
type: 'Entire house',
town: 'Some town',
title: 'Some title',
price: 50,
description: 'Some description',
guests: 4,
bedrooms: 1,
beds: 2,
baths: 1,
wifi: true,
reviewsCount: 2
},
[
{
id: 1,
houseId: 2329,
userId: 1,
comment: 'An awesome review',
createdAt: '2019-01-11T22:00:00.000Z',
updatedAt: '2019-01-11T22:00:00.000Z'
},
{
id: 2,
houseId: 2329,
userId: 2,
comment: 'Another awesome review',
createdAt: '2019-01-11T22:00:00.000Z',
updatedAt: '2019-01-11T22:00:00.000Z'
}
]
]
You're not using Promise.all correctly. The code is working, because you're awaiting each promise individually.
Since Review.findAndCountAll depends on House.findByPk result, Promise.all won't do any good here.
You're using Promise.all with the already resolved values of the both promises, so you can just drop it.
server.get("/api/houses/:id", async (req, res) => {
const { id } = req.params;
const housePromise = await House.findByPk(id);
const reviews = await Review.findAndCountAll({
where: {
houseId: house.id
}
});
house.dataValues.reviewsCount = reviews.count;
res.send([house.dataValues, reviews.rows]);
});
Basically you're doing:
const res = await Promise.all([1, 5]); // [1, 5]
Which can be translated directly to:
const res = [1, 5];
Instead of sending it in an array, I think it's better to send an object:
{
house: house.dataValues,
reviews: reviews.rows
}
You can ignore async await and use Promise. You can try following code
server.get("/api/houses/:id", async (req, res) => {
const { id } = req.params;
return House.findByPk(id)
.then( house => {
// !house might be 'true' if house is 'undefined'
if( house === undefined || !house ) {
return res.status(400).send("No house found");
}
return Review.findAndCountAll({ where: { houseId: house.id } })
.then(reviews => {
house.dataValues.reviewsCount = reviews.count;
return {house, reviews};
})
.catch(error => res.send(error));
})
.then( result => {
return res.send(results);
})
.catch(error => {
return res.send(error);
});
})

NoedJS forEach with add new element on a http-request

how to add new element in an array with a http request.
I have a code like this but it doesn't add new element because of async on nodejs and I don't know how can I pass it.
arr = [
{ id: 123},
{ id: 124},
{ id: 125},
{ id: 126},
]
arr.forEach(function(row, index) {
request.post('/test')
.then((data) => {
row.status = "success"
})
.catch((error) => {
row.status = "failed"
});
});
so that i can achieve something like this.
[
{ id: 123, status: 'success' },
{ id: 124, status: 'failed' },
{ id: 125, status: 'failed' },
{ id: 126, status: 'success' },
]
I'm new in NodeJs. thank you guys
You have to use Promise.all because you're handling several promises:
let arr = [
{ id: 123},
{ id: 124},
{ id: 125},
{ id: 126}
]
Promise.all(arr.map((row, index) => {
return request.post('/test')
.then(data => {
row.status = "success"
})
.catch(error => {
row.status = "failed"
});
})).then(() => console.log(arr))
You can try this popular node module Async. Try this .each here http://caolan.github.io/async/docs.html#each.
async.each(arr, _your_function, (err) => {
// check success
})
_your_function(){}
use async.eachOf, you can access element and index in the array.
var async = require("async");
var arr = [
{ id: 123},
{ id: 124},
{ id: 125},
{ id: 126},
];
async.eachOf(arr, function(e, i, ecb){
request.post('/test',)
.then( (data) => {
e.status = "success"
return ecb(null);
})
.catch( (error) => {
e.status = "failed"
return ecb(null);
});
}, function(err){
if(err)
{
console.log("err");
}
else
{
console.log(arr);
}
});

Resources