即使在流星中自动发布后,客户端也无法获得该集合

时间:2018-10-05 13:28:19

标签: mongodb meteor

/lib/collection.js

import { Mongo} from 'meteor/mongo'; 
export const Chats = new Mongo.Collection('chats');

/server/app.js

import { Chats} from '../lib/collections';

const chats = [
{
  text: 'You on your way?',

},
{
  text: 'Hey, it\'s me',

},
{
  text: 'I should buy a boat',

},
{
  text: 'Look at my mukluks!',

},
{
    text: 'This is wicked good ice cream.',
      }
];

chats.forEach(function(chat)
{
Chats.insert(chat);
})

/client/index.js

 import { Meteor } from 'meteor/meteor';


 import {Chats} from '../lib/collections'

console.log(Chats.find());

为什么Chats.find()返回空对象?

自动发布尚未删除。所以我无法得到错误。

1 个答案:

答案 0 :(得分:1)

这仅仅是因为数据需要一定的时间才能到达客户端。在您的/client/index.js

中尝试一下
import { Meteor } from 'meteor/meteor';
import { Tracker } from 'meteor/tracker';
import {Chats} from '../lib/collections'

Tracker.autorun( () => console.log( Chats.find() ) );