[英]Angular / Meteor: Array is undefined
I'm building an Angular / Meteor application, using Meteor's reactivity to populated an Angular model. 我正在构建一个Angular / Meteor应用程序,使用Meteor的反应性来填充Angular模型。 It is all working fine until I start working with arrays.
在开始使用数组之前,一切都很好。
// get sentence
$scope.sentence = $meteor.object(Sentences, $stateParams.sentenceId);
// related sentences
$scope.relatedSentences = $meteor.collection(function() {
return Sentences.find({_id: { $in: $scope.sentence.relatedSentences }});
});
I get this error on the JavaScript console 我在JavaScript控制台上收到此错误
Error: $in needs an array
at Error (native)
at Object.ELEMENT_OPERATORS.$in.compileElementSelector (http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:1894:15)
at http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:1576:19
at Function._.each._.forEach (http://localhost:3000/packages/underscore.js? 0a80a8623e1b40b5df5a05582f288ddd586eaa18:164:22)
In the Mongo / Meteor console 在Mongo / Meteor控制台中
meteor:PRIMARY> db.sentences.find({_id: "sp75iWPNqpbp2ypmy"});
{ "_id" : "sp75iWPNqpbp2ypmy", "sentence" : "Here is sentence 1 ", "language" : "en", "level" : "A1", "public" : true, "relatedSentences" : [ "wNs4ByDq7t396WLM3" ] }
And 和
meteor:PRIMARY> db.sentences.find({_id: { $in: [ "wNs4ByDq7t396WLM3" ] }});
{ "_id" : "wNs4ByDq7t396WLM3", "sentence" : "Here is sentence 0 ", "language" : "en", "level" : "A1", "public" : true, "relatedSentences" : [ ] }
I then commented out the failing find and have outputted the objects to html 然后,我注释掉失败的发现,并将对象输出到html
$scope.sentence = $meteor.object(Sentences, $stateParams.sentenceId);
$scope.test = typeof $scope.sentence.relatedSentences;
Sentence: {{sentence}}<br/>
Related: {{sentence.relatedSentences}}<br/>
Typeof: {{test}}
Results 结果
Sentence: {"autorunComputation": {"stopped":false,"invalidated":false,"firstRun":false,"_id":47,"_onInvalidateCallbacks": [null,null],"_parent":null,"_recomputing":false},"_id":"oFMp7swYyQsXkYsKz","sent ence":"Here is sentence 2","language":"en","level":"A1","public":true,"relatedSentences": ["wNs4ByDq7t396WLM3"]}
Related: ["wNs4ByDq7t396WLM3"]
Typeof: undefined
The Array is marked as undefined , but it is clearly shown in the full object. Array被标记为undefined ,但是在完整对象中清楚地显示了该数组。 What am I missing?
我想念什么?
EDIT When running the above test on Firefox it returns an object 编辑在Firefox上运行上述测试时,它将返回一个对象
Related: ["wNs4ByDq7t396WLM3"]
Typeof: object
$scope.test
is only bound one time when this code initially runs. 最初运行此代码时,
$scope.test
仅绑定一次。 There is no data binding, so even if $scope.sentence.relatedSentences
changes, test
will not. 没有数据绑定,因此即使
$scope.sentence.relatedSentences
更改, test
也不会更改。 The issue is that $scope.sentence.relatedSentences
is populated asynchronously. 问题是
$scope.sentence.relatedSentences
是异步填充的。
This would also apply to $scope.relatedSentences
-- you are trying to set it initially before $scope.sentence.relatedSentences
is available. 这也适用于
$scope.relatedSentences
您试图在$scope.sentence.relatedSentences
可用之前进行初始设置。 You can use .subscribe
on the value returned from .object
to set this once the data is available. 您可以使用
.subscribe
从返回的值.object
设置此一次的数据是可用的。
$scope.sentence.subscribe().then(function () {
$scope.relatedSentences = $meteor.collection(function() {
return Sentences.find({_id: { $in: $scope.sentence.relatedSentences }});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.