【问题标题】:Meteor subscribe is not loading data from the serverMeteor 订阅未从服务器加载数据
【发布时间】:2018-06-06 23:52:47
【问题描述】:

我在使用流星 1.6 时遇到问题。首先,我创建了一个数据库实例并尝试在客户端进行订阅。通过表格,我可以将数据输入到我的数据库中。但无法通过订阅检索它。谁能告诉我我的代码做错了什么?

import { Template } from "meteor/templating";
import { Notes } from "../lib/collection";
import { Meteor } from "meteor/meteor";
// import { ReactiveDict } from 'meteor/reactive-dict';
import "./main.html";
/* 
Template.body.onCreated(function bodyOnCreated() {
  this.state = new ReactiveDict();

  Meteor.subscribe("db1");
}); */

Template.Display.helpers({

  notes() {
    Meteor.subscribe("db1");
    return Meteor.call('data');
  }
});

Template.body.events({
  "click .delete": function() {
    Notes.remove(this._id);
  },

  "submit .formSubmit": function(event) {
    event.preventDefault();
    let target = event.target;
    let name = target.name.value;

    Meteor.call("inputs", name);

    target.name.value = "";

    return false;
  },
  "click .userDetail": function() {
    if (confirm("Delete the user Detail ?")) {
      Notes.remove(this._id);
    }
  }
});

这里是发布代码:

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

Meteor.methods({
    inputs:(name)=> {
      if (!Meteor.user()) {
        throw Meteor.Error("Logged in");
      }
      Notes.insert({
        name: name,
        createdAt: new Date()
      });
    },
    data:()=>{
        return Notes.find({});
    }
  });

【问题讨论】:

    标签: meteor meteor-blaze


    【解决方案1】:

    Meteor.subscribe("notes"); 应该在Template.body.onCreated lifycycle 方法中。你需要写一个 单独发布代码,而不是在Meteor.method 内。见下面的格式,

    Meteor.publish('notes', function tasksPublication() {
        return Notes.find({});
    });
    

    在 helper 内部,只需在下面调用订阅的 Collection,

    Template.Display.helpers({
      notes() {
        return Notes.find({});
      }
    });
    

    **注意:** 切勿在辅助方法中使用Meteor.callhelpers 是反应式和实时的。

    【讨论】:

      猜你喜欢
      • 2013-05-21
      • 2020-06-23
      • 1970-01-01
      • 2016-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-25
      相关资源
      最近更新 更多