Meteor 流星:订阅不起作用
我正在尝试构建一个非常简单的流星应用程序。我已经实现了所有CRUD。但我的问题是,当我使用发布然后订阅时,不会返回任何数据。我的代码如下: 导入\api\tasks\tasks.jsMeteor 流星:订阅不起作用,meteor,Meteor,我正在尝试构建一个非常简单的流星应用程序。我已经实现了所有CRUD。但我的问题是,当我使用发布然后订阅时,不会返回任何数据。我的代码如下: 导入\api\tasks\tasks.js import { Mongo } from 'meteor/mongo'; import { Meteor } from 'meteor/meteor'; TasksSchema = new SimpleSchema({ title: { type: String }, owner: {
import { Mongo } from 'meteor/mongo';
import { Meteor } from 'meteor/meteor';
TasksSchema = new SimpleSchema({
title: {
type: String
},
owner: {
type: String
}
});
export const Tasks = new Mongo.Collection('tasks', { schema: TasksSchema });
if (Meteor.isServer) {
Meteor.publish('task.list', function() {
return Tasks.find({ owner: this.userId });
});
}
import { Tasks } from '/imports/api/tasks/tasks.js';
import { Meteor } from 'meteor/meteor';
import { Template } from 'meteor/templating'
import { ReactiveDict } from 'meteor/reactive-dict';
import './home.html';
if (Meteor.isClient) {
Template.home.onCreated(function bodyOnCreated() {
Meteor.subscribe('task.list');
});
Template.home.helpers({
tasks() {
let userId = Meteor.userId();
return Tasks.find({ owner: userId }, { sort: { updatedAt: -1 } });
}
});
}
导入\ui\components\home\home.js
import { Mongo } from 'meteor/mongo';
import { Meteor } from 'meteor/meteor';
TasksSchema = new SimpleSchema({
title: {
type: String
},
owner: {
type: String
}
});
export const Tasks = new Mongo.Collection('tasks', { schema: TasksSchema });
if (Meteor.isServer) {
Meteor.publish('task.list', function() {
return Tasks.find({ owner: this.userId });
});
}
import { Tasks } from '/imports/api/tasks/tasks.js';
import { Meteor } from 'meteor/meteor';
import { Template } from 'meteor/templating'
import { ReactiveDict } from 'meteor/reactive-dict';
import './home.html';
if (Meteor.isClient) {
Template.home.onCreated(function bodyOnCreated() {
Meteor.subscribe('task.list');
});
Template.home.helpers({
tasks() {
let userId = Meteor.userId();
return Tasks.find({ owner: userId }, { sort: { updatedAt: -1 } });
}
});
}
导入\ui\components\home\home.html
<!-- here nothing is shown,although I have data -->
{{#each task in tasks }}
{{task.title}}
{{/each}}
{{{#任务中的每个任务}
{{task.title}}
{{/每个}}
有什么建议吗?提前感谢。请不要使用
{{task.title}
尝试{{title}}
并检查浏览器控制台任务。查找({}).fetch()并查看是否可以查看记录。这可能是因为您的订阅尚未准备好在客户端上使用。
您需要做的是将每个代码包装到subscriptionsReady
函数中,如下所示:
{{#if Template.subscriptionsReady}}
{{#each task in tasks }}
{{task.title}}
{{/each}}
{{else}}
<p>Loading...</p>
{{/if}}
{{#if Template.subscriptionsReady}
{{{#任务中的每个任务}
{{task.title}}
{{/每个}}
{{else}
加载
{{/if}
上面的方法应该有效。
有关如何使用subscriptionsReady
而不是{task.title}
try{{title}}
的详细信息,您可以查看链接,也可以查看浏览器控制台task.find({}).fetch()
并查看记录@Sasikanth是否有效,谢谢我把它贴出来作为答案,你可以接受它来结束这个问题。