使用Meteor无法将变量正确呈现为HTML(可能是数据上下文问题)

时间:2017-01-08 03:22:10

标签: javascript html meteor meteor-blaze

我正在尝试在我的网页中为每个用户的帖子创建一个链接。我已成功为每个帖子创建新页面(message.html)(此网页的“Hello World”正确显示,请参见下文)。但是,与我的主页相比,我想要呈现到HTML中的变量无法显示。提前谢谢!

注意: 使用Flow Router完成路由。

以下是代码的相关摘要:

Message.js (或html的控制器)

import { Meteor } from 'meteor/meteor';
import { Template } from 'meteor/templating';
import { Boosts } from '../api/boosts.js';

import './boost.js';
import './message.html';

Template.ApplicationLayout2.onCreated(function messagesOnCreated() {
  Meteor.subscribe('messages');
});

Template.boostPage.helpers({
messages() {
  return Boosts.findOne(this._id());
  },
});

Message.html (或打开新页面时显示的html)

注意:此html中的 Hello World 是作为可以在浏览器中有效显示的内容的示例。

<template name="ApplicationLayout2">
Hello World
    {{> boostPage}}
</template>

<template name="boostPage">
    {{#with messages}}
    <h2>Boost: {{text}}</h2>
    <q class="message">{{message}}</q>
    {{/with}}
</template>

Boosts.js(相关部分)(Meteor.publish('boosts',函数boostsPublication()...)与此问题无关,但我将其包含在内以保持代码的原始格式)

import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { check } from 'meteor/check';
import { Match } from 'meteor/check';

export const Boosts = new Mongo.Collection('boosts'); 

if (Meteor.isServer) {
  Meteor.publish('boosts', function boostsPublication() {
    return Boosts.find({
      $or: [
        { private: { $ne: true } },
        { owner: this.userId },
      ],
    });
  });
  Meteor.publish('messages', function messagesPublication() {
    return Boosts.find({
        {_id: this._id},
    });
  });
}

Routes.js

FlowRouter.route('/', {
  name:'home',
  action() {
    BlazeLayout.render('ApplicationLayout');
  }
});


FlowRouter.route('/boosts/:_id', {
  name:'messages',
  action: function() {
    BlazeLayout.render("ApplicationLayout2", {_id: this._id});
  }
});

编辑#1 我收录了zim建议的更正。

Message.js (或html的控制器)

import { Meteor } from 'meteor/meteor';
import { Template } from 'meteor/templating';
import { Boosts } from '../api/boosts.js';
import { ReactiveVar } from 'meteor/reactive-var';

import './boost.js';
import './message.html';

Template.Message.onCreated(function() {
    // get the message id from the route parameter
    let messageId = FlowRouter.getParam('messageid');

    // save it to a reactive var so the helper can use it
    this.messageId = new ReactiveVar(messageId);

    // subscribe to 1 message using that message id
    this.subscribe('message', messageId);
});

Template.Message.helpers({
    // get data for 1 message
    message() {
        let messageId = Template.instance().messageId.get();
        return Boosts.find({_id: messageId});
    }
});

Message.html (或打开新页面时显示的html)

注意: Hello World Boost:标题以及引号之间包含的 object object 将显示在浏览器中。

<template name="Message">
Hello World
    {{#with message}}
    <h2>Boost: {{text}}</h2>
    <q class="message">{{message}}</q>
    {{/with}}
</template>

Boosts.js(相关部分)(Meteor.publish('boosts',函数boostsPublication()...)与此问题无关,但我将其包含在内以保持代码的原始格式)

import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { check } from 'meteor/check';
import { Match } from 'meteor/check';

export const Boosts = new Mongo.Collection('boosts');

if (Meteor.isServer) {
  // This code only runs on the server
  // Only publish boosts that are public or belong to the current user
  Meteor.publish('boosts', function boostsPublication() {
    return Boosts.find({
      $or: [
        { private: { $ne: true } },
        { owner: this.userId },
      ],
    });
  });
    // Publish only the message corresponding to the boost in question
    Meteor.publish('message', function(messageId) {
    return Boosts.find({_id: messageId});
  });
}

Routes.js

  FlowRouter.route('/', {
  name:'home',
  action() {
    BlazeLayout.render('ApplicationLayout');
  }
});


FlowRouter.route('/boosts/:messageid', {
  name:'messages',
  action: function() {
    BlazeLayout.render("Message", {messageid: this._id});
  }
});

1 个答案:

答案 0 :(得分:1)

在此发布中:

  Meteor.publish('messages', function messagesPublication() {
    return Boosts.find({
        {_id: this._id},
    });
  });

...你期待什么&#34; this._id&#34;代表什么?你把它记录到控制台了吗?我怀疑它未定义。

更新

让我们说你有这样的路线:/ message /:messageid

你可以拥有一个这样的消息模板:

Message.html:

<template name="Message">
    {{#with message}}
      {{content}}    <!-- or some field within message -->
    {{/with}}
</template>

Message.js:

Template.Message.onCreated(function() {
    // get the message id from the route parameter
    let messageId = FlowRouter.getParam('messageid');

    // save it to a reactive var so the helper can use it
    this.messageId = new ReactiveVar(messageId);

    // subscribe to 1 message using that message id
    this.subscribe('message', messageId);
});

Template.Message.helpers({
    // get data for 1 message
    message() {
        let messageId = Template.instance().messageId.get();
        return Boosts.find({_id: messageId});
    }
});

这是一个简单的模板,用于获取单个邮件的数据并显示它。现在你需要发布那条消息:

server.js:

  Meteor.publish('message', function(messageId) {
    return Boosts.find({_id: messageId});
  });

这是一种做法。注意流程:路由器定义消息的id,模板订阅它,告诉服务器发布它,然后帮助程序查找发布的数据。

您还可以创建一个循环显示消息的模板。在这里,我们订阅所有消息数据,并将其全部发布。 (注意我如何用单数和复数命名以帮助传达概念)

Messages.html:

<template name="Messages">
    {{#each message in messages}}
      {{message.content}}    <!-- or some field within message -->
    {{/each}}
</template>

Messages.js:

Template.Messages.onCreated(function() {
    // subscribe to all the messages
    this.subscribe('messages');
});

Template.Messages.helpers({
    // get data for all the messages
    messages() {
        return Boosts.find();
    }
});

这会发布所有数据:

server.js:

  Meteor.publish('messages', function() {
    return Boosts.find();
  });

在这个例子中,我们不会从路由中提取任何信息,而是循环遍历所有消息。

有帮助吗?