How do I retrieve the newly added appointment from the MongoDB immediately after completion? (node/express)

Hi, I have added a custom field (a dropdownlist) 'Client' to the event editor. All appointments have that field. What I want is that whenever the user saves a new event, it gets hold of the last added document. I tried to do that by adding this in the onActionComplete() method:


scheduler.component.ts:

lastAppointment;
onActionComplete(args: ActionEventArgs): void {
this.eventService.getLastAppointment().subscribe((data : any) => {
this.lastAppointment = data.appointment;
});


console.log('last appointment: ', this.lastAppointment[0].Client);
}

This is on the Nodejs backend to retrieve the last document in the :
events.js:
router.get("/getLastAppointment", authRequired, (req, res) => {
    // console.log(req.decoded.userId);
User.findOne({_id: req.decoded.userId}).select('username email').exec((err, user) =>{
if(err){
res.json({success: false, message: err});
} else {
if(!user){
res.json({success: false, message: 'Not found'});
} else {
user= user;

connection.db.collection('ScheduleData').find().sort( { _id : -1 } ).limit(1).toArray((err, cus) => {
res.json({appointment: cus});
console.log(req.body.params);
});;
console.log(user.username);
}
}
});

});

However, when I save an appointment, it does not take the Client of the newly created appointment , but the last one before the user clicked on save, if that makes sense.
So if I make a new event and chose Bob , i want that lastAppointment; gets assigned to 'Bob'.
How do I get the 'Client' of the last created event? 

onPopupOpen() method:
onPopupOpen(args: PopupOpenEventArgs): void {

let clientsDrop =
[{text:'Hans', value:'Hans'}, {text:'Bob', value:'Bob'} ]
 

if (args.type === 'Editor') {
// Create required custom elements in initial time
if (!args.element.querySelector('.custom-field-row')) {

let row: HTMLElement = createElement('div', {className: 'custom-field-row'});
let formElement: HTMLElement = args.element.querySelector('.e-schedule-form');
formElement.firstChild.insertBefore(row, args.element.querySelector('.e-title-location-row'));
let container: HTMLElement = createElement('div', {className: 'custom-field-container'});

let inputEle: HTMLInputElement = createElement('input', {
className: 'e-field', attrs: {name: 'Client'}
})
as HTMLInputElement;
container.appendChild(inputEle);

let row2: HTMLElement = createElement('div', {className: 'custom-field-row'});
let formElement2: HTMLElement = args.element.querySelector('.e-schedule-form');
formElement2.firstChild.insertBefore(row, args.element.querySelector('.e-title-location-row'));
let container2: HTMLElement = createElement('div', {className: 'custom-field-container'});




row.appendChild(container);
row2.appendChild(container2);



let dropDownList: DropDownList = new DropDownList({

dataSource: clientsDrop,
fields: {text: 'text', value: 'value'},
value: (<{ [key: string]: Object }>(args.data)).Client as string,
floatLabelType: 'Always', placeholder: 'Client'
});

dropDownList.appendTo(inputEle);
inputEle.setAttribute('name', 'Client');


}
}
}



Thanks in advance.

1 Reply

HB Hareesh Balasubramanian Syncfusion Team March 23, 2020 10:43 AM UTC

Hi Youssef, 

Thanks for the update. 

We have validated your reported problem at our side by preparing CURD sample with MongoDB as service. In that we have using Dropdown Component as additional (custom) field and it can be downloaded from the following link. 

Code snippet
ngOnInit(): void { 
    this.selectedDate = new Date(2018, 1, 14); 
    this.eventSettings = { dataSource: this.dataManager }; 
    this.dropDownDataSource = [ 
      { company: 'Public Event', companyValue: 'public-event' }, 
      { company: 'Maintenance', companyValue: 'maintenance' }, 
      { company: 'Commercial Event', companyValue: 'commercial-event' }, 
      { company: 'Family Event', companyValue: 'family-event' } 
    ]; 
  } 
  onPopupOpen(args: PopupOpenEventArgs): void { 
    if (args.type === 'Editor') { 
      // Create required custom elements in initial time 
      if (!args.element.querySelector('.custom-field-row')) { 
        let row: HTMLElement = createElement('div', { className: 'custom-field-row' }); 
        let formElement: HTMLElement = <HTMLElement>args.element.querySelector('.e-schedule-form'); 
        formElement.firstChild.insertBefore(row, args.element.querySelector('.e-title-location-row')); 
        let container: HTMLElement = createElement('div', { className: 'custom-field-container' }); 
        let inputEle: HTMLInputElement = createElement('input', { 
          className: 'e-field', attrs: { name: 'EventType' } 
        }) as HTMLInputElement; 
        container.appendChild(inputEle); 
        row.appendChild(container); 
        let drowDownList: DropDownList = new DropDownList({ 
          dataSource: this.dropDownDataSource, 
          fields: { text: 'company', value: 'companyValue' }, 
          value: (args.data as { [key: string]: Object }).EventType as string, 
          floatLabelType: 'Always', placeholder: 'Event Type' 
        }); 
        drowDownList.appendTo(inputEle); 
        inputEle.setAttribute('name', 'EventType'); 
      } 
    } 
  } 


Kindly try the above sample and if you have any other concerns please revert for further assistance. 

Regards, 
Hareesh 


Loader.
Up arrow icon