.$id 检索但 .name 等不检索 - AngularFire
.$id retrieves but .name etc doesn't - AngularFire
我正在尝试通过从 routeparams 获取 $id 来从 firabase 检索数据,然后作为对象同步并使用 data.(childnamehere) 检索数据的各个部分。这在检索元数据时成功,即 data.$id 但在检索我自己的数据时不成功,即 data.name.. 我已经查看了文档,但无法弄清楚为什么我不能 console.log (在这种情况)courseUnique.name?任何帮助将不胜感激。
else if (action === "view") {
var courseId = $routeParams.id;
var sync = $firebase(coursesRef.child(courseId));
var courseUnique = sync.$asObject();
var id = courseUnique.$id;
var name = courseUnique.name;
console.log(courseId, courseUnique, id, name);
上面的控制台输出是:
-JhgqkeFyfvS9nh5bKyK FirebaseObject {$$conf: Object, $id: "-JhgqkeFyfvS9nh5bKyK", $priority: null, $save: function, $remove: function…} undefined
很抱歉回答这么简洁,但这个问题已经被讨论过很多次了。我稍后会提供一些指向先前答案的链接,但现在这段代码可以使用:
var courseUnique = sync.$asObject();
courseUnique.$loaded().then(function(courseUnique) {
console.log(courseUnique.name);
})
参见:
- the AngularFire documentation for the
$loaded
event
- this blog post explaining the
$loaded
event
- this answer explaining why you need this
$loaded
construct
- most of these questions are probably about the same topic
我正在尝试通过从 routeparams 获取 $id 来从 firabase 检索数据,然后作为对象同步并使用 data.(childnamehere) 检索数据的各个部分。这在检索元数据时成功,即 data.$id 但在检索我自己的数据时不成功,即 data.name.. 我已经查看了文档,但无法弄清楚为什么我不能 console.log (在这种情况)courseUnique.name?任何帮助将不胜感激。
else if (action === "view") {
var courseId = $routeParams.id;
var sync = $firebase(coursesRef.child(courseId));
var courseUnique = sync.$asObject();
var id = courseUnique.$id;
var name = courseUnique.name;
console.log(courseId, courseUnique, id, name);
上面的控制台输出是:
-JhgqkeFyfvS9nh5bKyK FirebaseObject {$$conf: Object, $id: "-JhgqkeFyfvS9nh5bKyK", $priority: null, $save: function, $remove: function…} undefined
很抱歉回答这么简洁,但这个问题已经被讨论过很多次了。我稍后会提供一些指向先前答案的链接,但现在这段代码可以使用:
var courseUnique = sync.$asObject();
courseUnique.$loaded().then(function(courseUnique) {
console.log(courseUnique.name);
})
参见:
- the AngularFire documentation for the
$loaded
event - this blog post explaining the
$loaded
event - this answer explaining why you need this
$loaded
construct - most of these questions are probably about the same topic