奇怪的问题,插入文档后,它存在一瞬间,然后自我删除? [英] Weird issue where after inserting a doc, it exists for an instant, and then deletes itself?

查看:23
本文介绍了奇怪的问题,插入文档后,它存在一瞬间,然后自我删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我测试的方式是在模板中使用一个简单的 for 循环来遍历客户端可用的元素并将它们显示在列表中.

They way I'm testing this is a simple for loop in the template to run through the elements available to the client and display them in a list.

我通过由 #query 标识的文本输入插入元素.

I insert the elements through a text input identified by #query.

当我输入一个元素时,它会显示一小段时间,并且打印出 Links.find().fetch() 的控制台日志显示该元素存在,然后不久之后,该元素似乎是自动删除的,对 Links.find().fetch() 的任何连续调用都会产生一个空列表.这是 Meteor 中的错误吗?或者是预期的行为和糟糕的实施?

When I enter an element, it displays for a brief instant, and a console log that prints out Links.find().fetch() shows that the element exists, and then shortly afterwards, the element is seemingly automagically removed making any successive calls to Links.find().fetch() yield an empty list. Is this a bug within Meteor? Or is it expected behaviour and bad implementation?

更新

另一个奇怪的开发,我在服务器端添加了 setTimeout(function(){Links.find().fetch()},3000); 来尝试跟踪正在发生的事情.使用这一行,插入可以正常工作一段时间,然后因以下错误而崩溃:http://i.imgur.com/CUYDO67.png.这是怎么回事?

Another weird development, I added setTimeout(function(){Links.find().fetch()},3000); to the server side to try and track what was going on. With this line, the inserts work correctly for a while, and then crashes with these errors: http://i.imgur.com/CUYDO67.png . What is going on?

下面是我的模板文件 myapp.html

<head>
  <title>myapp</title>
</head>

<body>
  {{> search_bar}}
  <br>
  {{> list_of_links}}
</body>

<template name="search_bar">
    <h1>Playlist</h1>
    <input id="query" type="text" placeholder="Enter Query Here"/>
</template>

<template name="list_of_links">
   <ul id="item-list">
        {{#each my_playlist}}
            {{> link_item}}
        {{/each}}
    </ul>
</template>

<template name="link_item">
<li class="link">
    <div class="link-title">{{youtube_link}}&nbsp;&nbsp;&nbsp;&nbsp;{{sess}}</div>
</li>
</template>

这里是 myapp.js

//Setting up a collection of urls
Links = new Meteor.Collection("links");

if (Meteor.isClient) {
  //"Subscribing" to server's published data
    Deps.autorun( function(){
      Meteor.subscribe( "links", Meteor.default_connection._lastSessionId);
    });

  //Nuke database helper function -- debugging
  Template.list_of_links.clean = function(collection) {
    if(collection) {
        // clean items
        _.each(collection.find().fetch(), function(item){
            collection.remove({_id: item._id});
        });
    }
  }

  //Songs from session
    Template.list_of_links.my_playlist = function () {
      return Links.find();
    };

    Template.search_bar.events({
    //http://stackoverflow.com/a/13945912/765409
    'keypress #query' : function (evt,template) {
      // template data, if any, is available in 'this'
      if (evt.which === 13){
        var url = template.find('#query').value;
        //Find a nicer way of clearing shit.
        $("#query").val('');
        Links.insert({sess:Meteor.default_connection._lastSessionId,youtube_link:url});
        var cursor = Links.find();
        cursor.rewind();
        console.log(cursor.fetch());
        //Add to database.
    }
    }
  });  

  }

if (Meteor.isServer) {
  Meteor.startup(function () {
    // code to run on server at startup
    Meteor.publish("links", function( sess ) {
      return Links.find({sess: sess});  //each client will only have links with that _lastSessionId
    });
    //Making sure permissions are correct
Links.allow({
  insert: function (userId, doc) {
    return true;
  }
}); 
  });
}

推荐答案

当用户没有足够的权限来创建文档时,会出现这种行为.插入功能会立即创建文档的本地副本(由于延迟补偿),然后将其与服务器操作的结果同步.如果该操作失败,临时文档将从客户端的 Minimongo 中清除.

That kind of behavior is expected when user doesn't have enough privileges to create a document. The insert function creates a local copy of the doc instantly (thanks to latency compensation), and then sync it with the result of server operation. If that operation fails, the temporary document is purged from client's Minimongo.

您是否使用 Collection.allow 创建了正确的规则?这是寻找原因的第一个地方.

Have you created proper rules with Collection.allow? That's the first place to look for the cause.

这篇关于奇怪的问题,插入文档后,它存在一瞬间,然后自我删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆