【问题标题】:Meteor: Application crashing流星:应用程序崩溃
【发布时间】:2015-09-02 16:51:48
【问题描述】:

我在其中一个控制器中有以下方法。当我使用{{ getLastMessage }} 从html 代码调用此方法时,浏览器崩溃了。多次调用此方法,浏览器无响应。有人可以帮忙解决这个问题吗?

$scope.getLastMessage = function(userId) {
   var query = {};
   query['uid'] = userId;
   var lastMessage = $meteor.collection(function(){
      return Chats.find(query, {fields: {'_id':1, 'content':1, 'uid':1}});
   });
   console.log(lastMessage);
   return lastMessage;
};

【问题讨论】:

    标签: javascript meteor angular-meteor meteor-collection2


    【解决方案1】:

    我认为你的问题是 getLastMessage 是一个函数,而不是一个属性,所以你的代码相当于 {{ function(){} }} 甚至永远不会被调用。您需要实际调用 {{getLastMessage()}} 之类的函数或立即调用控制器上的函数。

    如果我可以为您提供一个稍微简单一些的解决方案(尽管效率可能会降低):

    如果您尚未将集合绑定到范围变量,您可能需要执行以下操作:

    // SERVER CODE -- put in your /server directory
    
    // NB: I am only publishing the specific user's (uid) chats in case someone 
    //     finds this through Google and by accident creates a vulnerability
    //     unintentionally. Feel free to change the Mongo query if you want to     
    //     publish all chats to all users 
    
    Meteor.publish("Chats", function () {
      return Chats.find({uid:this.userId}, {fields: {'_id': 1,'content': 1,'uid': 1}});
    });
    
    // CLIENT CODE
    
    $scope.chatsCollection = $scope.$meteorCollection("Chats").subscribe("Chats");
    // lastMessage should be updated reacitvely -- no need to run a function
    $scope.lastMessage = $scope.chatsCollection.slice(-1)[0];
    

    话虽如此,切片假设 Meteor Collection 按时间顺序将新文档附加到末尾,因此现实可能并不那么简单。 $scope.chatsCollection 包含数组的所有方法,因此您可以对其进行排序或使用 underscore.js 之类的东西对其执行查询。

    您可能会考虑采用的另一种方法是使用纯 Meteor Cursor.observe method - 看起来您在最初的方法中有点遵循。这里的一个好处是您可以对 Mongo 查询执行任何必要的排序操作,这可能会更有效。

    我认为这看起来像:

    // CLIENT SIDE CODE
    // Initial assignment 
    $scope.lastMessage = Chats.find({uid:Meteor.userId(), {
                    fields: {
                        '_id': 1,
                        'content': 1,
                        'uid': 1
                    },
                    sort: {INSERT YOUR SORT HERE, e.g. by creation time}
                })
            .fetch().slice(-1).pop();
    
    
    
    // Subscription to changes made on the query
       Chats.find({uid:Meteor.userId(), {
                fields: {
                    '_id': 1,
                    'content': 1,
                    'uid': 1
                },
                sort: {INSERT YOUR SORT HERE, e.g. by creation time}
            })
        .observe({added: function(document){$scope.lastMessage = document}});
    

    【讨论】:

    • @NutanKumarLade 如果该编辑使提供的答案无效(由 JacobWuzHere 确认),那么它不是合法的编辑,应该回滚。请不要进行使现有答案无效的编辑。
    • 谢谢凯尔。 @NutanKumarLade 你的问题现在真的很不一样了,所以建议你恢复编辑并重新打开一个新问题。无论如何,我提出的任何一个解决方案都有效吗?如果没有,可能是因为 Meteor.users 查询——by default the Accounts system 只允许用户访问他自己的 Accounts 文档——即使这样也只能访问某些字段,如用户名、电子邮件和个人资料。如果您想为您的用例发布其他字段,我建议您创建一个名为“onlineUsers”的单独集合发布
    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    • 2020-09-25
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    • 2018-03-16
    相关资源
    最近更新 更多