当前位置: 动力学知识库 > 问答 > 编程问答 >

json - Can't use the data I fetch from parse.com

问题描述:

I have created a simple program to illustrate my problem.

I have a custom class called TestDate which has 2 columns and the default columns one column is a string called val the is a date column called date.

I create a row and set 'date' to new Date('1st April 2013') and I set 'val' to 'First String'

Then I saved it and it saves ok.

Next I retrieve the row using fetch I am able to display the record in the console window of the browser by using JSON.stringify(result)

If though I try to access specific values with JSON.stringify(result.results) I get undefined.

I have tried JSON.parse that hasn't worked I have also tried to access other data in the result object without any success.

It's as if the retrieved data has to converted somehow to be proper json but I do not know how? This is the problem.

my code is shown below:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Date Test</title>

</head>

<body>

<button onclick="fetch()">Fetch</button>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<script src="//www.parsecdn.com/js/parse-1.5.0.min.js"></script>

<script>

(function() {

Parse.initialize($PARSE_APPLICATION_ID, "$PARSE_JAVASCRIPT_KEY");

var TestDate = Parse.Object.extend("TestDate");

var testDate = new TestDate();

testDate.set({

'date': new Date('2013, 4, 1'),

'val': 'First String'

}).save(null, {

success: function(result) {

console.log("Inside create: success: result: " + JSON.stringify(result));

// Result in browser console is:

// Inside create: success: result:

// {"date":{"__type":"Date","iso":"2013-03-31T23:00:00.000Z"},

// "val":"First String","objectId":"Nq2PmNH0yN",

// "createdAt":"2015-09-14T09:04:58.487Z",

// "updatedAt":"2015-09-14T09:04:58.487Z"}

},

error: function(obj, error) {

console.log("Inside create: error: obj: " + JSON.stringfify(obj) +

" error: " + JSON.stringify(error));

}

});

}());

function fetch() {

var TestDate = Parse.Object.extend("TestDate");

var testDate = new TestDate();

testDate.fetch({

success: function(result) {

console.log("Inside fetch: success: result: " + JSON.stringify(result));

// Result in browser console is:

// Inside fetch: success: result:

// {"results":[{"createdAt":"2015-09-14T09:04:58.487Z",

// "date":{"__type":"Date","iso":"2013-03-31T23:00:00.000Z"},

// "objectId":"Nq2PmNH0yN","updatedAt":"2015-09-14T09:04:58.487Z",

// "val":"First String"}]}

console.log("Inside fetch: success: result.results: " + JSON.stringify(result.results));

// The result in the browser shows the problem I get back undefined:

// Inside fetch: success: result.results: undefined

},

error: function(obj, error) {

console.log("Inside fetch: error: obj" + JSON.stringify(obj) +

" error " + JSON.stringify(error));

}

})

}

</script>

</body>

</html>

网友答案:
var date = result.get("date");
var val = result.get("val");

or

var date = result.attributes.date;
var val = result.attributes.val;
分享给朋友:
您可能感兴趣的文章:
随机阅读: