【发布时间】:2015-09-14 09:59:15
【问题描述】:
我创建了一个简单的程序来说明我的问题。
我有一个名为 TestDate 的自定义类,它有 2 列,默认列一列是名为 val 的字符串,是一个名为 date 的日期列。
我创建一行并将“日期”设置为新日期(“2013 年 4 月 1 日”),并将“瓦尔”设置为“第一个字符串”
然后我保存了它,它保存好了。
接下来我使用 fetch 检索该行 我可以使用 JSON.stringify(result) 在浏览器的控制台窗口中显示记录
如果我尝试使用 JSON.stringify(result.results) 访问特定值,我得到未定义。
我尝试了 JSON.parse 但没有成功我还尝试访问结果对象中的其他数据但没有任何成功。
就好像检索到的数据必须以某种方式转换为正确的 json 但我不知道如何?这就是问题所在。
我的代码如下所示:
<!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>
【问题讨论】:
标签: json parse-platform