一个出版物对另一出版物隐藏嵌套字段 [英] One publication is hiding nested fields from another publication

查看:21
本文介绍了一个出版物对另一出版物隐藏嵌套字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定名为 Foo 的同一个集合的两个出版物.fooList 应该只返回特定的字段,而 fooDetail 应该返回整个文档.

Given two publications for the same collection named Foo. The fooList should only return specific fields but the fooDetail should return the whole document.

服务器:

Meteor.publish 'fooList', ->
    return Foo.find(
        {}
        { fields:
            foo: true
            'bar.bas': true
        })

Meteor.publish 'fooDetail', (foo_id) ->
    return Foo.find _id: foo_id

客户:

Meteor.subscribe 'fooList'
Meteor.subscribe 'fooDetail', some_id

我希望在订阅 fooDetail 时获得完整的 foo.但是bar"(嵌套文档)中的所有字段都不可用,bar.bas"字段除外.

I expected to get the complete foo when subscribing to fooDetail. But all the fields from 'bar' (the nested document) are not available, except the 'bar.bas' field.

这是一个错误还是流星应该这样工作?(我目前使用的是 Meteor 1.0.3.2 和 Iron-Router)

Is this a bug or should meteor work this way? (I'm currently on Meteor 1.0.3.2 and Iron-Router)

推荐答案

这不是错误,而是一个 已知限制 流星的MergeBox.这是大多数流星开发人员曾经困扰过的那些令人困惑的问题之一.

This isn't a bug, it's a known limitation of the meteor's MergeBox. It's one of those confusing problems that bites most meteor developers once.

来自文档:

如果多个订阅发送了一个字段的冲突值(相同的集合名称、文档 ID 和字段名称),则客户端上的值将是任意选择的已发布值之一.

If more than one subscription sends conflicting values for a field (same collection name, document ID, and field name), then the value on the client will be one of the published values, chosen arbitrarily.

你可以看到这篇文章 可能的解决方法.在您的示例中,您可以将发布修改为:

You can see this post for possible workarounds. In your example, you could modify your publish to look like:

Meteor.publish 'fooList', ->
  Foo.find {}, fields: foo: 1, bar: 1

这将发布所有顶级 bar 字段,以避免冲突,但在您的特定用例中可能不可接受.

That would publish all of the top-level bar field which avoids the conflict but may not be acceptable in your particular use case.

这篇关于一个出版物对另一出版物隐藏嵌套字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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