0

我正在使用backbone.js 创建一个phonegap 应用程序。我正在保存到数据库并获得对象响应:

    match_user.save({
            'email':user.email,
            'password':password,
            'username':username,
            'facebook_login':'yes',
            'facebook_id':user.id
    }, 
    {
    success:function(data){
        console.log('data is ');
        console.log(data);

                console.log('data.username is ');
                console.log(data.username);

                console.log('data.password is ');
                console.log(data.password);

        console.log('JSON.stringify(data) is ');
        console.log(JSON.stringify(data));



        that.setSession(data);
    },
    error:function(){
        console.log('in the error');
    }
    });

eclipse中console.log的输出是:

    10-16 14:01:08.490: I/Web Console(10185): data is  
    10-16 14:01:08.490: I/Web Console(10185): [object Object]

            10-16 14:20:51.500: I/Web Console(11297): data.username is  
        10-16 14:20:51.505: I/Web Console(11297): undefined

        10-16 14:20:51.510: I/Web Console(11297): data.password is  
        10-16 14:20:51.510: I/Web Console(11297): undefined 

    10-16 14:01:08.495: I/Web Console(10185): JSON.stringify(data) is
    {"first_name":"",
    "last_name":"",
    "email":"test@test.com",
    "username":"user5654401630",
    "password":"ipk9d",
    "facebook_login":"yes",
    "facebook_id":"5675685685"} 

data.username、data.email 等都返回“未定义”,但是当我对对象进行字符串化时,我可以看到它们具有值...

4

2 回答 2

1

那是因为 JSON.stringify 返回一个字符串并且您不再将 json 作为对象所以您需要获取数据而不使用相同的字符串

 match_user.save({
            'email':user.email,
            'password':password,
            'username':username,
            'facebook_login':'yes',
            'facebook_id':user.id
    }, 
    {
    success:function(data){
        console.log('data is ');
        console.log(data);

        console.log('JSON.stringify(data) is ');
        console.log(data);

        console.log('JSON.stringify(data).username is ');
        console.log(data.username);

        var obj = JSON.parse(data);
        console.log('obj.username is ');
        console.log(obj.username);

        that.setSession(data);
    },
    error:function(){
        console.log('in the error');
    }
    });
于 2013-10-16T13:14:48.370 回答
0

实际上,这是一个骨干的事情。data.username 返回 undefined 但 data.get('username') 有效。

于 2013-10-16T13:36:41.087 回答