【问题标题】:Meteor using a local connection results in error: insert failed: 404 -- Method not foundMeteor 使用本地连接导致错误:插入失败:404 -- 找不到方法
【发布时间】:2023-03-26 01:16:01
【问题描述】:

我在客户端有一个流星集合

Friends = new Meteor.Collection("Friends");
Meteor.subscribe("Friends");

我有一个用户通过 facebook 进行身份验证,我想获取他们的朋友列表:

FB.api("/me/friends?    auth_token="+response.authResponse.accessToken,
    function(response){
        for (i = 0; i<response.data.length;i++){
            Friends.insert(response.data[i]);
    }
);

我有一个函数可以获取该列表:

Template.Friends.all_friends = function(){
    return Friends.find();
} 

我有一个模板想在屏幕上显示所有朋友:

<template name="Friends">
    {{#each all_friends}}
    <div id="{{id}}" class="friend">
        <img src="http://graph.facebook.com/{{id}}/picture" />
        {{name}}
    </div>
    {{/each}}
</template>

页面上似乎正在发生的事情是所有朋友确实在屏幕上闪现片刻,然后屏幕立即闪回空白。

在 javascript 控制台中,我的每个朋友都会显示一次消息(是的,它大于零,感谢提问)

insert failed: 404 -- Method not found

所以!我错过了什么?有人吗?

【问题讨论】:

    标签: collections client-side meteor


    【解决方案1】:

    您需要在客户端和服务器上都使用 Collection 声明。

    // common code, do not put under /client or inside Meteor.is_client test
    Friends = new Meteor.Collection("Friends");
    

    【讨论】:

      【解决方案2】:

      如果您只想在客户端使用 Collection,并且不需要将该数据保存到服务器,您可以在“client”文件夹或 .isClient() 函数中声明您的集合,方法是像这样将 null 传递给构造函数:

      if(Meteor.isClient()){
      // Some other code
      ...
      
      onlyClientCollection = new Meteor.Collection(null);
      
      // Some other code
      ...
      }
      

      【讨论】:

      • 这件事让我很困扰。感谢您的回答
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-02
      • 1970-01-01
      • 2018-07-04
      • 2020-02-08
      • 1970-01-01
      • 2020-06-05
      相关资源
      最近更新 更多