7 Replies - 876 Views - Last Post: 24 August 2017 - 04:04 PM

#1 Nitewalkr  Icon User is offline

  • D.I.C Lover

Reputation: 149
  • View blog
  • Posts: 1,045
  • Joined: 17-November 10

Having issue with ObjectId (using Angular cli)

Posted 14 July 2017 - 03:54 AM

Hi there,

It has been a long time since I have posted here. I am having an issue with Update and Delete function(s) as Delete works with errors and Update doesn't work at all.

When I try deleting an event, it gets deleted but then gives an error, and when I refresh the app it no longer appears on the calendar.

When I try updating an event, the values get passed perfectly but gives an error and does not update.

At first it was giving and id of undefined when I tested it out using console.log(), I have tried assigning the event id to the id that is assigned in the interface and it gets displayed but when I the function it gives the following error.

TypeError:'Cast of ObjectId failed for value "undefined" at path "_id"'

import { Component, OnInit } from '@angular/core';
import { ChangeDetectorRef } from '@angular/core';
import { StudentService } from '../../services/student.service';
import { Student } from '../../interfaces/students';

import { ScheduleModule } from 'primeng/primeng';
import { DialogModule } from 'primeng/primeng';
import { InputTextModule } from 'primeng/primeng';
import { ButtonModule } from 'primeng/primeng';
import { DropdownModule, SelectItem } from 'primeng/primeng';
import { RadioButtonModule } from 'primeng/primeng';


@Component({
  moduleId: module.id,
  selector: 'app-students',
  templateUrl: './students.component.html',
  styleUrls: ['./students.component.css']
})
export class StudentsComponent implements OnInit {
  
  students: any[];
  student: Student;
  
  header: any;
  
  locations: SelectItem[];
  selectedLocation: string;
  selectedValue: string;
  
  dialogVisible: boolean = false;
  
  MorningIsChecked: number = 30;
  AfternoonIsChecked: number = 30;
  EveningIsChecked: number = 30;

  //isEdit:boolean = false;
  
  constructor(private studentService:StudentService, private cd: ChangeDetectorRef ) {  
    
    this.locations = [];
    this.locations.push({label:'Brampton', value: 'Brampton'})
    this.locations.push({label:'Confederation', value: 'Confederation'})
    this.locations.push({label:'North York', value: 'North York'})
    this.locations.push({label:'Calgary', value: 'Calgary'})
    this.locations.push({label:'Vancouver', value: 'Vancouver'})
  }

  ngOnInit(){
    this.studentService.getStudents().subscribe(students => {
      this.students = students;
      console.log(students);
    });
    
    this.header = {
      left: 'prev,next today',
      center: 'title',
      right: 'month,agendaWeek,agendaDay'
    };
  }
  
  handleDayClick(event){
    this.student = new Student();
    
    this.student.start = event.date;
    
    this.dialogVisible = true;
    
    this.cd.detectChanges();
  }
  handleEventClick(event){
    this.student = new Student();
    console.log(this.student);
    this.student._id = event.calEvent._id;
    this.student.title = event.calEvent.title;
    this.student.email = event.calEvent.email;
    this.student.event = event.calEvent.event;
    this.student.location = this.selectedLocation;
    this.student.time = this.selectedValue;
    this.dialogVisible = true;
  }

  updateEvent(student){
    student._id = this.student._id;
    console.log(this.student);
    this.studentService.updateStudent(this.student).subscribe(student => {
      for(let i = 0; i < this.students.length; i++){
        if(this.student[i]._id == this.student._id){
          this.students.splice(i,1);
        }
      }
      this.students.push(this.student);
    });
    this.dialogVisible = false;
  }

  saveEvent(){
    this.student.location = this.selectedLocation;
    this.student.time = this.selectedValue;
    this.studentService.addStudent(this.student).subscribe(student => {
      this.students.push(student);
      console.log(student);
      
    });
    this.dialogVisible = false;
  }
  deleteEvent(_id){
    _id = this.student._id;
    console.log(_id);
    this.studentService.deleteStudent(_id).subscribe(res => {
      for(let i = 0; i < this.students.length; i++){
        if(this.student[i]._id == _id){
          this.students.splice(i,1);
        }
      }
    });
    this.dialogVisible = false;
  }
  
}



Screenshots:

Posted Image

Posted Image

Posted Image



I would appreciate if someone can help me with this

Is This A Good Question/Topic? 0
  • +

Replies To: Having issue with ObjectId (using Angular cli)

#2 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1829
  • View blog
  • Posts: 5,761
  • Joined: 15-January 14

Re: Having issue with ObjectId (using Angular cli)

Posted 14 July 2017 - 10:34 AM

It sounds like those error messages are referring to either this.student._id or this.student[i]._id, but you should be able to figure out which by checking the line numbers.
Was This Post Helpful? 0
  • +
  • -

#3 Nitewalkr  Icon User is offline

  • D.I.C Lover

Reputation: 149
  • View blog
  • Posts: 1,045
  • Joined: 17-November 10

Re: Having issue with ObjectId (using Angular cli)

Posted 14 July 2017 - 12:10 PM

The id is appropriately being passed I think as, in console log it shows the id of the object it is going to delete.
Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1829
  • View blog
  • Posts: 5,761
  • Joined: 15-January 14

Re: Having issue with ObjectId (using Angular cli)

Posted 14 July 2017 - 03:41 PM

That's fine, but the error message only applies to one of the 2 lines I mentioned. It is saying you are trying to access a property called _id on an undefined variable, and those are the only places where you access _id. That means that one of them is undefined.
Was This Post Helpful? 0
  • +
  • -

#5 Nitewalkr  Icon User is offline

  • D.I.C Lover

Reputation: 149
  • View blog
  • Posts: 1,045
  • Joined: 17-November 10

Re: Having issue with ObjectId (using Angular cli)

Posted 14 July 2017 - 10:01 PM

I believe that i found the issue. it is the if statement, the matching of the _id with this.student[i]._id this gives an error of undefined. When I console log the _id that I am passing from the top it does show, because I am logging the object that I select as well.

more specifically:

for(var i = 0; i < this.students.length; i++)
        {
          console.log(_id);
          if(this.student[i]._id == _id)
          {
            this.students.splice(this.student[i]._id,1);
            //this.students.splice(i,1);
          }
          console.log(this.student[i]._id);
        }



this.student[i]._id


this line is giving error.
I have not found any solution for this so far.

This post has been edited by Nitewalkr: 14 July 2017 - 10:07 PM

Was This Post Helpful? 0
  • +
  • -

#6 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1829
  • View blog
  • Posts: 5,761
  • Joined: 15-January 14

Re: Having issue with ObjectId (using Angular cli)

Posted 17 July 2017 - 10:41 AM

Have you verified what this.student is set to? Should that be this.students?
Was This Post Helpful? 1
  • +
  • -

#7 david19922  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 08-July 17

Re: Having issue with ObjectId (using Angular cli)

Posted 18 July 2017 - 10:49 PM

id is appropriately being passed I think as, in console log it shows the id of the object it is going to delete
Was This Post Helpful? 1
  • +
  • -

#8 Nitewalkr  Icon User is offline

  • D.I.C Lover

Reputation: 149
  • View blog
  • Posts: 1,045
  • Joined: 17-November 10

Re: Having issue with ObjectId (using Angular cli)

Posted 24 August 2017 - 04:04 PM

This issue was resolved. I went with id instead, the issue was, it will not take the index which I was trying to compare, find the id using the forloop and then returning the appropriate id. There is no way to do it without a click event. It worked fine without the id. It got deleted with no errors, and I had to use 'GET' method to refresh the page. For the update, I couldnt find a proper resource to update, using the appropriate id, so I later on didnt go with it. There was this idea of deleting the values and adding them with the same id but it was pretty far fetched...

I apologize for the late reply, but Later on, after the project was done, the client already had someone else build the entire project behind my back, so I decided not to pursue it any further.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1