在我的代码中,我需要更新模型
{
"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"
}
]
}
我使用了promise all来实现这一目标。在邮递员中,我发送此对象,但首先需要添加客户,联系人数组,然后添加传递地址数组。我做了如下。
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;
});
}
在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;
}
}
问题在于,当它承诺所有方法并且我需要获取最后更新的客户时,它不能解决所有方法,但是它给出了结果DeliveryAddress和带有空数组的联系人。根据需要更新mongodb上的客户文档。
答案 0 :(得分:0)
您需要直接在平面数组中传递承诺。
如果iterable包含非承诺值,它们将被忽略,但仍计入返回的promise数组值中(如果已实现promise)
您可以使用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(/* ... */
此外,由于您已经在使用async
/ await
,所以没有理由不能await
Promise.all
调用。
const results = await Promise.all([...updatedAddress, ...updatedContacts]);
console.log(results);
return this.customerRepo.getLastUpdatedCustomer();
答案 1 :(得分:0)
您还可以嵌套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])