Meteor - 在列表对象中呈现所有者的名称

2024-03-16

当我尝试查找对象的所有者时,出现“未找到对象”错误 我正在尝试渲染。我正在循环播放一组视频剪辑,这些剪辑可以由用户更新或管理。当我登录时,代码工作正常,但是当我尝试使用它并注销时,我收到“排队任务中的​​异常:TypeError:无法读取 Object.Template.video_info 中未定义的属性“_id”。创建者姓名“

我尝试通过这样做来调试它:

console.log(this.owner);
var owner = Meteor.users.findOne(this.owner);
console.log(owner);

当我检查控制台日志时,我可以看到找到了正确的用户 ID,当我使用此 ID 手动运行 Meteor.users.findOne 时,我得到了一个返回的用户对象。 Meteor 中的时间安排有什么奇怪的地方阻止了这种情况发生吗?

更新:如果我向模板creatorname函数添加一个try...catch,那么会记录2个错误,但模板仍然呈现...???似乎这个模板被调用了两次,一次是在它还没有准备好时,另一次是在它准备好时。为什么会这样呢。

try...catch 块的示例:

  Template.video_info.creatorName = function () {
      try{
        var owner = Meteor.users.findOne(this.owner);
        if (owner._id === Meteor.userId())
          return "me";
        return displayName(owner);
      } catch (e){
        console.log(e);
      }
  };

低于此点的原始损坏代码

这是我的 HTML 中的内容:

<body>
  <div>
    {{> video_list}}
  </div>
</body>
<template name="video_list">
  <h1>Video List</h1>
  {{#each videos}}
    <ul>
      {{> video_info}}
    </ul>
  {{else}}
    No videos yet.
  {{/each}}
  <div class="footer">
    <button>Like!</button>
  </div>
</template>

<template name="video_info">
  <li class="video-list {{maybe_selected}}">
    <img src="{{image}}" />
    <div>
      <h3>{{title}}</h3>
      <p>{{description}}</p>
      <h4>{{creatorName}}</h4>
    </div>
  </li>
</template>

这是在我的 client.js 中

Meteor.subscribe("videos");

if (Meteor.isClient) {

  Template.video_list.videos = function() {
    return Videos.find({}, {sort: {title: 1}});
  };

  Template.video_list.events = {
    'click button': function(){
      Videos.update(Session.get('session_video'),{$inc: {likes: 1}});
    }
  }

  Template.video_info.maybe_selected = function() {
    return Session.equals('session_video', this._id) ? "selected" : "";
  }

  Template.video_info.events = {
    'click': function(){
      Session.set('session_video', this._id);
    }
  }

  Template.video_info.creatorName = function () {
    var owner = Meteor.users.findOne(this.owner);
    if (owner._id === Meteor.userId())
      return "me";
    return displayName(owner);
  };
}

if (Meteor.isServer) {
  Meteor.startup(function () {
    // code to run on server at startup
  });
}

这是在我的 model.js 中

Videos = new Meteor.Collection("videos");

Videos.allow({
  insert: function (userId, video) {
    return false; // no cowboy inserts -- use createParty method
  },
  update: function (userId, video, fields, modifier) {
    if (userId !== video.owner)
      return false; // not the owner

    var allowed = ["title", "description", "videoid", "image", "start"];
    if (_.difference(fields, allowed).length)
      return false; // tried to write to forbidden field

    // A good improvement would be to validate the type of the new
    // value of the field (and if a string, the length.) In the
    // future Meteor will have a schema system to makes that easier.
    return true;
  },
  remove: function (userId, video) {
    // You can only remove parties that you created and nobody is going to.
    return video.owner === userId; //&& attending(video) === 0;
  }
});

var NonEmptyString = Match.Where(function (x) {
  check(x, String);
  return x.length !== 0;
});

var NonEmptyNumber = Match.Where(function (x) {
  check(x, Number);
  return x.length !== 0;
});

createVideo = function (options) {
  var id = Random.id();
  Meteor.call('createVideo', _.extend({ _id: id }, options));
  return id;
};

Meteor.methods({
  // options should include: title, description, x, y, public
  createVideo: function (options) {
    check(options, {
      title: NonEmptyString,
      description: NonEmptyString,
      videoid: NonEmptyString,
      image:NonEmptyString,
      start: NonEmptyNumber,
      _id: Match.Optional(NonEmptyString)
    });

    if (options.title.length > 100)
      throw new Meteor.Error(413, "Title too long");
    if (options.description.length > 1000)
      throw new Meteor.Error(413, "Description too long");
    if (! this.userId)
      throw new Meteor.Error(403, "You must be logged in");

    var id = options._id || Random.id();
    Videos.insert({
      _id: id,
      owner: this.userId,
      videoid: options.videoid,
      image: options.image,
      start: options.start,
      title: options.title,
      description: options.description,
      public: !! options.public,
      invited: [],
      rsvps: []
    });
    return id;
  },

});

///////////////////////////////////////////////////////////////////////////////
// Users

displayName = function (user) {
  if (user.profile && user.profile.name)
    return user.profile.name;
  return user.emails[0].address;
};

var contactEmail = function (user) {
  if (user.emails && user.emails.length)
    return user.emails[0].address;
  if (user.services && user.services.facebook && user.services.facebook.email)
    return user.services.facebook.email;
  return null;
};

我想我已经找到了这个问题的解决方案。在阅读了 Meteor 中的缓存工作之后,我发现了订阅模型以及它与 Meteor minimongo 的关系http://docs.meteor.com/#dataandsecurity http://docs.meteor.com/#dataandsecurity。失败然后成功的原因是在第一次加载时数据仍然缓存在 minimongo 中。我目前正在检查配置的帐户登录服务,以检查用户数据是否已加载。我目前正在使用它,因为我找不到订阅 Metor 用户服务的方法,但我的猜测是帐户登录服务将依赖于 Metor 用户集合。我当前的解决方案如下所示:

if(Accounts.loginServicesConfigured()){
  var owner = Meteor.users.findOne(this.owner);
  if (owner._id === Meteor.userId())
    return "me";
  return displayName(owner);
}

目前这似乎工作正常。我仍在研究如何订阅此用户服务。在寻找此解决方案时我发现了一些非常有用的参考资料

  • https://github.com/oortcloud/unofficial-meteor-faq https://github.com/oortcloud/unofficial-meteor-faq
  • http://psychopyko.com/cool-stuff/meteor-6-simple-tips/ http://psychopyko.com/cool-stuff/meteor-6-simple-tips/
  • https://groups.google.com/forum/#!topic/meteor-talk/QKXe7qfBfqg https://groups.google.com/forum/#!topic/meteor-talk/QKXe7qfBfqg
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Meteor - 在列表对象中呈现所有者的名称 的相关文章

  • 邮件附件媒体类型错误 Gmail API

    我正在尝试通过 Javascript 客户端中的 Gmail API 发送带有附加 jpeg 文件的消息 到目前为止我写的代码如下 ajax type POST url https www googleapis com upload gma
  • browserify 错误 /usr/bin/env: 节点: 没有这样的文件或目录

    我通过 apt get install 安装了 node js 和 npm 以及所有依赖项 然后安装了 browserify npm install browserify g 它完成了整个过程 看起来安装正确 但是当我尝试为此做一个简单的捆
  • 允许指针(单击)事件穿过元素,同时保持滚动功能

    我的目标是拥有一个允许 下面要点击 交互的元素 滚动 众所周知 1 的解是pointer events none 这正如中所描述的单击 DIV 到底层元素 https stackoverflow com questions 3680429
  • 如何制作像Stackoverflow一样的可折叠评论框

    我正在构建一个网站 并且有一个状态更新列表 我希望允许用户为列表中的每个项目撰写评论 但是我正在尝试实现一个类似于堆栈溢出工作方式的用户界面 特别是可折叠的评论表单 列表 用户在其中单击对列表中的特定状态更新添加评论 并且在列表中的该项目下
  • 通过 HTML5 文件和 URL API 正确创建和提供 PDF Blob

    好吧 假设我有文档数据存储在某处 让我们任意取this pdf http www grida no climate ipcc tar wg1 pdf tar 01 pdf 问题 1 我想要做的是对此 URL 进行 AJAX 调用 因为我需要
  • Chrome 扩展程序中的后台脚本到底何时运行?

    在我的 chrome 扩展中 我有一个后台脚本 它将使用XMLHttpRequest note that this code is in the global scope i e outside of any function also n
  • 为什么“dtoa.c”包含这么多代码?

    我将是第一个承认我对低级编程的整体知识有点稀疏的人 我理解许多核心概念 但我不经常使用它们 话虽这么说 我对需要多少代码感到非常惊讶dtoa c http www netlib org fp dtoa c 在过去的几个月里 我一直致力于用
  • React:React 如何确保在浏览器有机会绘制之后调用 useEffect?

    useLayoutEffect 的文档说 useLayoutEffect 内计划的更新将被刷新 在浏览器有机会绘制之前同步进行 useEffect 的文档说 与 componentDidMount 和 componentDidUpdate
  • 如何在 JavaScript 中构建一个计算数组中出现次数的对象?

    我想计算数组中某个数字出现的频率 例如 在Python中我可以使用Collections Counter创建一个字典 记录某个项目在列表中出现的频率 据我所知 JavaScript 是这样的 var array 1 4 4 5 5 7 va
  • 如何获取数组中最后 5 个元素(不包括第一个元素)?

    在 JavaScript 数组中 如何获取最后 5 个元素 排除第一个元素 1 55 77 88 would return 55 77 88 添加其他示例 1 55 77 88 99 22 33 44 would return 88 99
  • NodeJS 无法加载 css 文件

    所以我正在尝试制作一个 NodeJS 服务器 并且我尝试保留尽可能少的附加组件 但是 我遇到了一个问题 我似乎无法加载任何内容CSS我调用的文件HTML文件 该调用似乎确实由服务器处理 但它不会显示在浏览器中 My 网络服务器 js fil
  • 如何在参数上使用 .reduce() 而不是特定的数组或对象?

    我想定义一个函数 flatten 将多个元素展平为一个数组 我知道以下是不可能的 但本质上我想这样做 var flatten function var flattened arguments reduce function acc elem
  • 用于导出到 CSV/Excel 的数据 URI(无服务器端请求):浏览器支持/限制?

    以下问题 Javascript 或 Flash 导出至 CSV Excel https stackoverflow com questions 8150516 javascript or flash export to csv excel
  • 禁用任何类型的浏览器窗口滚动?

    有没有办法禁用滚动 不仅仅是滚动条 还有浏览器窗口的全部功能 根据您对 Keit 的回答 您不想在打开灯箱时滚动处于活动状态 如果是这种情况 您可以使用以下 css 在打开灯箱的同时向正文添加一个类 这个解决方案的好处是它保留了滚动 空间
  • chrome 扩展 - 将数据从后台传递到自定义 html 页面

    创建浏览器扩展 我必须从 background js 打开新选项卡并将 JSON 数据传递到这个新选项卡 在新选项卡中 我使用传递的 JSON 数据来操作 渲染 DOM 下面是我的 background js 的一部分 我在其中使用自定义
  • IE6 丢失查询字符串

    我有一个使用 javascript 从查询字符串中获取值的页面window location 从网络服务器运行时效果很好 但如果我通过将其放在地址栏中使用 IE6 在本地运行它 c mysite index htm 网站创建的任何查询字符串
  • 盒式捆绑包与 MVC4 捆绑包

    我目前正在开发一个原型 ASP NET MVC 3 解决方案 该解决方案将用作多个项目重写的基础 来自 Web 表单 我的目标之一是跨应用程序实现一些脚本管理 而不是我们目前没有的目标 MVC 3有一个缺陷恕我直言 如果您需要在部分视图或模
  • 使用 JavaScript 从 URL 变量读取来加载不同的 CSS 样式表

    我试图在我的 WordPress 博客上使用两个不同的样式表 以便在通过 Web 访问页面时使用一个样式表 而在通过我们的 iOS 应用程序访问博客内容时使用另一个样式表 现在 我们将 app true 附加到来自 iOS 应用程序的 UR
  • 如何在 ChartJS 中创建自定义图例

    我需要使用 ChartJS 库为我的圆环图创建自定义图例 我已经使用 ChartJS 提供的默认图例创建了甜甜圈 但我需要一些修改 我希望其价值高于汽车名称 另外 我不喜欢粘性图例 我想将其与甜甜圈分开 这样我就可以更改字体 框的样式 例如
  • html5 canvas 使用图像作为蒙版

    是否可以使用具有形状的图像作为整个画布或画布内图像的蒙版 我想将图像放置在画布中 并在图像上添加蒙版 然后将其另存为新图像 您可以使用 source in globalCompositeOperation 将黑白图像用作蒙版 首先 将蒙版图

随机推荐