lundi 22 juin 2015

How to get JSON response from ember save method

I am creating a register function for my web app. On successful register the server saves the records to db and returns and auth token in the response, I am however unable to get the auth token from my response. Here is my register action in controller

registeruser2: function () {
  //  this.set('paneRadioClassFemale','radioActive');

  var self = this,
    data = this.getProperties('useremail', 'userpassword', "userfullname", 'mm', 'dd', 'yyyy', 'usercity', 'userstate', 'userzip', 'usergender');
  console.log(data);


  var registeruser = self.store.createRecord('registeruser', data);


  registeruser.save().then(function(response){
    console.log(response.get('token'));
    console.log(response.get('usertoken'));
    //self.set('token')
    self.transitionToRoute('userhomeselectbrand');
  });



},

Here is my Express Server function to persist the data

router.post('/registerusers',function(req,res){

  //  console.log(req.body);
  //  console.log(req.body.registeruser.useremail);
    var currentToken = hat();
    var hiveuser = new Hiveuser();
    var dateString=req.body.registeruser.yyyy+"/"+req.body.registeruser.mm+"/"+req.body.registeruser.dd;
    var dateOfBirth=new Date(dateString);
    hiveuser.email = req.body.registeruser.useremail;
    hiveuser.password = req.body.registeruser.userpassword;
    hiveuser.fullname = req.body.registeruser.userfullname;
    hiveuser.dateofbith=dateOfBirth;
    hiveuser.city=req.body.registeruser.usercity;
    hiveuser.state=req.body.registeruser.userstate;
    hiveuser.zip=req.body.registeruser.userzip;
    hiveuser.sex=req.body.registeruser.usergender;
    hiveuser.registertype="hive";
    hiveuser.authtoken=currentToken;
    console.log(currentToken);

    var userEmail=req.body.registeruser.useremail;

    Hiveuser.find({'email':userEmail},function(err,findResponse){


            hiveuser.save(function(err) {
                if (err)
                    res.send(err);
                authtoken=new AuthToken();
                authtoken.email=userEmail;
                authtoken.auth_token=currentToken;
                authtoken.created_date=new Date();
                authtoken.expiry_date=authtoken.created_date+160000;

                authtoken.save(function(err1){
                    if(err1)
                        res.send(err1);
                });
                res.send({'usertoken':{'success':true,'token':currentToken}});
            });

    });


}); 

I just want to retrieve the token returned by the Ember response. My browser console log for response shows undefined for both

        console.log(response.get('token'));
        console.log(response.get('usertoken'));

I even created an Ember model for usertoken but that is empty even after the promise is fullfilled.

Interestingly enough when I go to the Network tab in my Chrome inspector I see the correct intended response i.e.

{"usertoken":{"success":true,"token":"20cade39a77931d8a7e9facfd10c5803"}}

But unable to get the response in Ember data. If I do a console.log(response) I get the following data

Class {id: null, store: Class, container: Container, _changesToSync: Object, _deferredTriggers: Array[0]…}__ember1435038661483: "ember650"__ember_meta__: Object__nextSuper: undefined_attributes: Object_changesToSync: Object_data: Object_deferredTriggers: Array[0]_implicitRelationships: Object_inFlightAttributes: Object_recordArrays: ember$data$lib$system$ordered$set$$OrderedSet_relationships: Object_updatingRecordArraysLater: falsecontainer: ContainercurrentState: (...)get currentState: GETTER_FUNCTION() {set currentState: (val) {id: nullstore: ClasstoString: () {__proto__: Class

But nowhere my intended json ,stuck on this for the entire day,please help




Aucun commentaire:

Enregistrer un commentaire