Search code examples
node.jsangularmongodbionic2angular2-services

Reading response from MongoDB after updating the database


I am trying to read/use the response from mongo db after I create a new record in the db. I am using Node JS, Angular 2/4 and Ionic 3. I am successfully able to post the request, however, I am stuck at reading the response(I get the response in JSON) Screenshot of the response in my console response

Now in the js code, I am trying to get the value of FirstName. How do I access it ?

This is my ts code.

 CreateProfile(){
    this.Loader("Creating Account");   
    this.params={
    FirstName:this.createDetails.get('firstName').value,
    LastName:this.createDetails.get('lastName').value,
    City:this.createDetails.get('city').value,
    Dob:this.createDetails.get('myDate').value.formatted, //this.userDob, //this.formPersonal.get('dob').value,
    State:this.createDetails.get('state').value,
    EmailID:this.createAccount.get('email').value,
    Phone:this.createDetails.get('phone').value,
    Gender:this.createDetails.get('gender').value,
    Address:this.createDetails.get('address').value,
    PinCode:this.createDetails.get('pincode').value,
    Password:this.createAccount.get('password').value
    }
    this.NewProfile.createProfile(this.params).then(res=>{
      console.log(res);    

    })
  }

I am printing my response on the console.log(res) which you can see on the attached image, I am tyring to access the values I have tried res[0].FirstName I have tried res.FirstName I have got errors both times.

This is code from my router/provider file

public createProfile(options){
    return new Promise((resolve, reject) => {
        this.http.post(this.localUrl, options)
          .map(res => res.json())
          .subscribe(res => {
            resolve(res);
          }, (err) => {
            reject(err);
          });
    });
  }

I have removed all other codes irrelevant to this question. I am guessing this would be a very trivial issue but not able to figure out what I am missing here


Solution

  • I got the solution, I just had to use an array like this (below code)

    this.NewProfile.createProfile(this.params).then(res=>{
          console.log(res['FirstName']);  
                 })
    

    Didn't realize it was this straightforward