Promise all in typescript does not resolve all - mongodb

In my code I need to update the model
{
"customerCode": "CUS15168",
"customerName": "Adam Jenie",
"customerType": "Cash",
"printPackingSlip": "true",
"contacts": [
{
"firstName": "Hunt",
"lastName": "Barlow",
"email": "huntbarlow#volax.com",
"deliveryAddress": "805 Division Place, Waumandee, North Carolina, 537",
},
{
"firstName": "Barlow",
"lastName": "Hunt",
"email": "huntbarlow#volax.com",
"deliveryAddress": "805 Division Place, Waumandee, North Carolina, 537",
}
],
"deliveryAddress": [
{
"addressName": "Postal",
"addressType": "postal address",
"addressLine1": "plaza street",
"addressLine2": "broome street",
"suburb": "Guilford",
"city": "Oneida",
"state": "Colorado",
"postalCode": "3971",
"country": "Belarus",
"deliveryInstruction": "test delivery address"
},
{
"addressName": "Physical",
"addressType": "physical address",
"addressLine1": "plaza street",
"addressLine2": "broome street",
"suburb": "Guilford",
"city": "Oneida",
"state": "Colorado",
"postalCode": "3971",
"country": "Belarus",
"deliveryInstruction": "test delivery address"
}
]
}
I used promise all to achieve that. In postman, I send this object, but first it needs to add the customer, the contact array and then delivery address array. I did it as follows.
public async createCustomer(customer: CustomerDTO): Promise<CustomerDTO> {
let deliveryAddress = [];
let contacts = [];
let customerDto = new CustomerDTO();
customerDto.customerCode = customer.customerCode;
customerDto.tenantId = customer.tenantId;
if (customer.contacts.length > 0) {
customer.contacts.map((element => {
contacts.push(element);
}));
customer.contacts.length = 0;
}
if (customer.deliveryAddress.length > 0) {
customer.deliveryAddress.map((element => {
deliveryAddress.push(element);
}));
customer.deliveryAddress.length = 0;
}
const createdCustomer = await this.customerRepo.updateOrCreateCustomer(customer);
let updatedAddress = deliveryAddress.map(async (address: CustomerDeliveryAddressDto) => {
return await this.customerRepo.updateDeliveryAddress(address, customerDto, address._id);
});
let updatedContacts = contacts.map(async (contact: CustomerContactsDto) => {
return await this.customerRepo.createOrUpdateContactList(contact, customerDto, contact._id);
});
return Promise.all([updatedAddress, updatedContacts]).
then((results: [Promise<boolean>[], Promise<boolean>[]]) => {
console.log(results);
return this.customerRepo.getLastUpdatedCustomer();
}).
then((result) => {
return result;
}).
catch(e => {
console.error(e);
return e;
});
}
In customerRepository
public async updateDeliveryAddress(deliveryAddressDto: CustomerDeliveryAddressDto, customerDto: CustomerDTO, deliveryAddressId: string): Promise<boolean> {
const customerToBeUpdated = await this.model.findOne({
customerCode: customerDto.customerCode,
tenantId: customerDto.tenantId
});
if (customerToBeUpdated !== null) {
if (deliveryAddressId != null || deliveryAddressId != undefined) {
const result = await this.model.findOneAndUpdate({ _id: customerToBeUpdated._id, deliveryAddress: { $elemMatch: { _id: deliveryAddressId } } },
{
$set: {
//code here
}
},
{ 'new': true, 'safe': true, 'upsert': true });
if (result){
return true;
}
} else {
const result = await this.model.findOneAndUpdate({ _id: customerToBeUpdated._id },
{
$push: { deliveryAddress: deliveryAddressDto }
},
{ 'new': true, 'safe': true, 'upsert': true }
);
if (result) {
return true;
}
}
} else {
return false;
}
}
The problem is that it does not resolve all the methods when it goes to promise all method and I need to get the last updated customer, but it gives the result DeliveryAddress and contacts with empty arrays. Customer document on mongodb is updated as needed.

You need to pass the promises directly in a flat array.
Promise.all on MDN
If the iterable contains non-promise values, they will be ignored, but still counted in the returned promise array value (if the promise is fulfilled)
You can do this easily using the spread operator.
let updatedAddress = deliveryAddress.map(async (address: CustomerDeliveryAddressDto) => {
return await this.customerRepo.updateDeliveryAddress(address, customerDto, address._id);
});
let updatedContacts = contacts.map(async (contact: CustomerContactsDto) => {
return await this.customerRepo.createOrUpdateContactList(contact, customerDto, contact._id);
});
// need to give a flat array to Promise.all, so use the `...` spread operator.
return Promise.all([...updatedAddress, ...updatedContacts]).then(/* ... */
Also, since you are already using async / await, no reason you cannot await the Promise.all call.
const results = await Promise.all([...updatedAddress, ...updatedContacts]);
console.log(results);
return this.customerRepo.getLastUpdatedCustomer();

You can also nest Promise.all
let updatedAddress = Promise.all(deliveryAddress.map(async (address: CustomerDeliveryAddressDto) => {
return await this.customerRepo.updateDeliveryAddress(address, customerDto, address._id);
}));
let updatedContacts = Promise.all(contacts.map(async (contact: CustomerContactsDto) => {
return await this.customerRepo.createOrUpdateContactList(contact, customerDto, contact._id);
}));
return Promise.all([updatedAddress, updatedContacts])

Related

why mongoose populate() request does not work?

I try to populate some data from other collection to an other collection.i had googled the search and also i follow the tutorial step by step but the population had fail.any help is appreciate friends. this is the code:
router.get("/", passport.authenticate("jwt", {session: false}), (req, res)=> {
const errors = {};
Profile.findOne({user: req.user.id})
.then(profile => {
if (!profile) {
errors.noprofile = "there is no profile for this user"
return res.status(404).json(errors);
}
res.json(profile);
}).catch(err=> res.status(404).json(err))
});
// #route POST api/profile
//#desc Create or edit user profile
//#access Private
router.get("/", passport.authenticate("jwt", {session: false}), (req, res)=> {
const {errors, isValid} = validateProfileInput(req.body);
//Check validation
if(!isValid) {
return res.status(400).json(errors);
}
// Get profile data
const profileData = {};
profileData.user = req.user.id;
if(req.body.handle) {
profileData.handle = req.body.handle
};
if(req.body.company) {
profileData.company = req.body.company
};
if(req.body.website) {
profileData.website = req.body.website
};
if(req.body.location) {
profileData.location = req.body.location
};
if(req.body.status) {
profileData.status = req.body.status
};
if(typeof req.body.skills !== 'undefined') {
profileData.skills = req.body.skills.split(',');
}
//social
profileData.social = {};
if(req.body.youtube) {
profileData.social.youtube = req.body.youtube
};
if(req.body.twitter) {
profileData.social.twitter = req.body.twitter
};
if(req.body.facebook) {
profileData.social.facebook = req.body.facebook
};
if(req.body.instagram) {
profileData.social.instagram = req.body.instagram
};
Profile.findOne({user: req.user.id})
.populate(
"user",
["name, avatar"]
)
this is the result that I get from the postman :
"_id": "62ee1058ceb295ccdfedffce",
"user": "62e6825958870d3db69d2da5",
"handle": "pablo",
"status": "developper",
"skills": [
"design web"
],
and the correct result must be :
"_id": "62ee1058ceb295ccdfedffce",
"user": {"_id": "62e6825958870d3db69d2da5",
"name": "pablo",
"avatar": "//www.gravatar.com/avatar/1ffsrenbdgeajks-ghsdereys1dkkdhddbc"
}
"handle": "pablo",
"status": "developper",
"skills": [
"design web"
],

How to fix this issues unknown error show at this page

code for api_addEvent.dart
class ApiAddEventToCalender {
Future<ApiResponseData> addEventToCalender(
String token,
String date,
String time,
String previousEventDocID,
int eventDateEpocMilisecond) async {
ApiResponseData apiResponseData =
ApiResponseData(responseCode: "27", responseMsg: "Unknown Error");
var url = '${Constants.apiEndPoint}/addEventToCalender';
if (date.length < 1) {
ApiResponseData apiResponseData =
ApiResponseData(responseCode: "17", responseMsg: "Invalid date.");
return apiResponseData;
}
if (eventTitle.text.length < 1) {
ApiResponseData apiResponseData = ApiResponseData(
responseCode: "17", responseMsg: "Event title is required.");
return apiResponseData;
}
if (eventString.text.length < 1) {
ApiResponseData apiResponseData = ApiResponseData(
responseCode: "17", responseMsg: "Event description is required.");
return apiResponseData;
}
Map data = {
"Token": token,
"API": "AddEventToCalender",
"Data": {
"tokenType": "jwt",
"date": date,
"title": eventTitle.text,
"event": eventString.text,
"time": time,
"eventDateEpocMilisecond": eventDateEpocMilisecond
}
};
if (previousEventDocID.length > 10) {
data = {
"Token": token,
"API": "AddEventToCalender",
"Data": {
"tokenType": "jwt",
"date": date,
"title": eventTitle.text,
"event": eventString.text,
"time": time,
"eventDateEpocMilisecond": eventDateEpocMilisecond,
"previousEventDocID": previousEventDocID,
}
};
}
//encode Map to JSON
var body = json.encode(data);
var response = await http.post(Uri.parse(url),
headers: {"Content-Type": "application/json"}, body: body);
if (response.statusCode == 200) {
var result = json.decode(response.body.toString());
if (result['ResponseCode'] == "0") {
/*ApiResponseData(
responseCode: "17", responseMsg: 'Event added to calender.');
return apiResponseData; */
} else if (result['ResponseCode'] == "1") {
apiResponseData.responseCode = result['ResponseCode'].toString();
apiResponseData.responseMsg = result['ResponseMsg'].toString();
return apiResponseData;
}
}
return apiResponseData;
}
}
2nd code for call api_addEvent.dart
Future<void> _addEventToCalender(String date, String time,
int eventDateEpocMilisecond, String previousEventDocID) async {
setState(() {
_progressBarActive = true;
});
var result = await ApiAddEventToCalender().addEventToCalender(
widget.token, date, time, previousEventDocID, eventDateEpocMilisecond);
if (result.responseCode == "0") {
} else {
if (result.responseCode == "1") {
Navigator.of(context).pushNamedAndRemoveUntil(
'/social_signin', (Route<dynamic> route) => false);
} else {
showMessageDialogBox(context, result.responseMsg.toString());
}
}
setState(() {
_progressBarActive = false;
});
}
I want to ask how to fix this error message. I tried to fix it but still had no success. For example ApiResponseData (
responseCode: "17", responseMsg: "Event title is required."); will appear the message "Event title is required." if I do not enter a title. But for unknown errors, I'm not sure I should always show near this page. Can someone help me?

Group by and Get Max Value MongoDb

I would like to get the highest number of counts for each numId and display it on my front end in a table.
Here is an example of my database:
{
"_id": {
"$oid": "6294777f677b4c647e28771a"
},
"numId": "5",
"respondee": "0x9d95bcaa5b609fa97a7ec860bec115aa94f85ba9",
"__v": 0,
"originalResponse": "test2",
"submittedAt": {
"$date": {
"$numberLong": "1653897087357"
}
},
"addresses": [
"0x39c878a3df98002ddba477a7aa0609fb5a27e2ff",
"0xe3342d6522ad72f65d6b23f19b17e3fb12161f90"
],
"count": 2
},
{
"_id": {
"$oid": "6294836e677b4c647e287e93"
},
"numId": "5",
"respondee": "0xe3342d6522ad72f65d6b23f19b17e3fb12161f90",
"__v": 0,
"originalResponse": "test3",
"submittedAt": {
"$date": {
"$numberLong": "1653900142375"
}
},
"addresses": [
],
"count": 0
}
I have written something like this but I'm not sure how to group the results according to the numId
import Response from '../../../models/Response.model';
import db from '../../../utils/config/db';
import nc from 'next-connect';
import { onError } from '../../../utils/error';
const handler = nc({
onError,
});
//GET all
handler.get(async (req, res) => {
await db.connect();
let responses = await Response.find({ });
//To group responses by numId
// Sort responses by votes in ascending order
responses = responses.sort((a, b) => {
return a.count - b.count;
});
let topResponses = responses.filter((response) => {
return response.count === responses[0].count;
});
// Check if respondee has the highest count response
if (
topResponses.length > 0 &&
topResponses.find((response) => {
return response.respondee === respondee;
})
) {
// Get the response
let response = topResponses.find((response) => {
return response.respondee === respondee;
});
// Get the response
let responseString = response.response;
// Get the count
let count = response.count;
}
await db.disconnect();
});
export default handler;
I have figured out the answer by referring from another stackoverflow:
Group by and Get Max Value MongoDb
let responses = await Response.aggregate([
{ $sort: { votes: -1 } },
{ $group: { _id: '$baseId', group: { $first: '$$ROOT' } } },
{ $replaceRoot: { newRoot: '$group' } },
]);
res.send(responses);

How can I send updated object from subdocument array of objects using mongoose as response

I want to send updated object as response from subdocument.
From my carts model, I am querying the subdocument array which is cartItems. After querying and doing crud operation, I don't want to send the full cartItems array as response. I want to send the updated object as response.
exports.removeCartItem = async (req, res) => {
const { itemId } = req.params
const { email } = req.user
const { id } = req.body
const targetUser = await User.findOne({ email }).exec()
const cartItemRemoved = Cart.findOneAndUpdate(
{
user: targetUser._id,
},
{
$pull: { cartItems: { _id: itemId } },
},
{ new: true },
).exec((err, data) => {
if (err) {
console.log(er)
}
res.json(data)
})
}
This is the response I am getting right now:
{
"user": "621def0665c08eff01794f6e",
"cartItems": [
{
"product": "6228edb603d5e7ca773a2b04",
"quantity": 5,
"price": 200,
"_id": "622b04741bab0093c963ef18"
}
],
"_id": "622b04741bab0093c963ef17",
"__v": 0
}
I want send the updated object as response - something like below:
{
"product":"6228edb603d5e7ca773a2b04",
"quantity": 5,
"price": 200,
"_id": "622b04741bab0093c963ef18"
}

how to write findOneAndUpdate query in express.js?

i have shown my data , which is stored in database like this
{
"_id": {
"$oid": "5799995943d643600fabd6b7"
},
"Username": "xx",
"Email": "xx#gmail.com",
"Info": "Deactivate",
"Description": "aajdjdjddjdkjddjdjdhdj",
"VerificationCode": "594565",
"VerificationExpires": {
"$date": "2016-10-07T10:20:20.077Z"
}
}
My controller:
if Username, Email, Info are matched I need to update " Info = 'Active' " this is working at the same time i need to delete 'VerificationCode' field and 'VerificationExpires' field how can i achieve this?
exports.updatearticle = function(req, res) {
Article.findOneAndUpdate(
{ "Username":'xx', "Email":'xx#gmail.com', "Info": "Deactivate" },
{ "$set": { "Info": "Active" } },
{ "new": true }
function (err, doc) {
if (err) { // err: any errors that occurred
console.log(err);
} else { // doc: the document before updates are applied if `new: false`
console.log(doc); // , the document returned after updates if `new true`
console.log(doc.Info);
}
}
);
};
above condtion matched and info getting changed but i want to delete VerificationCode,VerificationExpires some one help me out
exports.updatearticle = function(req, res) {
Article.findOne( { "Username":'xx', "Email":'xx#gmail.com', "Info": "Deactivate" }, function(err, result){
if (!err && result) {
result.Info = "Active"; // update ur values goes here
result.VerificationCode = "";
result.VerificationExpires = {};
var article = new Article(result);
article.save(function(err, result2){
if(!err) {
res.send(result2);
} else res.send(err);
})
} else res.send(err);
});
}
home this may help