我需要找到一种方法来“监视”我的数据库中的随机文档,目前我正在试图通过使用以下内容来发布:
// Generate a random ID from our documents
function randomId() {
// Count total documents, then generate a random index
var count = Documents.find().count();
var random = Math.floor(Math.random() * count);
// Get our random document
var document = Documents.findOne({}, {
skip: random
});
// Return the Document ID
return document._id;
}
// Publish a random document
Meteor.publish("randomDocument", function(random) {
var documentId = randomDocumentId();
return Documents.find({
_id : documentId,
})
})
这很好用,当我决定要投票时,问题出现在用户界面中。我将按下投票按钮,但计数器不会在UI上增加或减少。目前我正在使用以下代码将订阅加载到$ scope中:
$scope.$meteorSubscribe('randomDocument', Random.fraction()).then(function(subHandle) {
// Load latest random document into scope
var documents = $meteor.collection(Documents)
$scope.document = documents[documents.length-1]
})
在UI中,我使用以下内容显示是投票:
span.details-pane-yes {{ document.votes }}
这是我要求增加投票的方法:
Documents.update(documentId, {
$inc: {
votes: 1
}
});
每当调用此UI时,UI都不会更新以反映它。当我从终端查询它时,它确实显示在数据库中。
我需要的是当任何用户对文档进行投票时,计数器会更新,而且无论我尝试什么,我似乎无法使其工作。任何帮助都将非常感谢!