【问题标题】:How to use chimpmail in Meteor + React?如何在 Meteor + React 中使用 chimpmail?
【发布时间】:2016-12-17 22:52:28
【问题描述】:

首先感谢您花时间阅读本文,我不得不说我是 Web 开发领域的新手。我一直要求在一个项目中实现 mailchimp(我从未听说过),为此我刚刚开始搜索有关 chimpmail API 的信息,我也按照 meteorchef 的教程进行操作,问题是我可以做到这一点工作,我发现用 React 实现这个非常困难。当我输入邮件并运行 onSubmit 方法时,我的方法出现问题,控制台输出显示“未找到方法句柄订阅者”。到目前为止,我只是从教程中制作了 handleSubscriber 部分,我没有显示任何电子邮件列表喷气机。我正在使用meteor --settings=settings.json 运行我的 Meteor 应用程序。我将非常感谢一些帮助。

更新

我收到了一条有用的评论,指出我应该将我的方法文件添加到我的服务器目录中。通过这样做,我不再得到“找不到方法句柄订阅者”,而不是我现在得到的 Exception while invoking method 'handleSubscriber' Error: Match error: Expected object, got null

这是我的 Meteor 包:

meteor-base
mobile-experience
mongo
blaze-html-templates
reactive-var
jquery
tracker
library

standard-minifier-css
standard-minifier-js    
es5-shim
ecmascript

react-meteor-data@0.2.6-beta.16
accounts-ui
accounts-password
practicalmeteor:mocha
miro:mailchimp
fortawesome:fontawesome
themeteorchef:bert
standard-app-packages
underscore
themeteorchef:jquery-validation
check

这是我的 settings.json

{
  "public": {},
  "private": {
     "MailChimp": {
       "apiKey": "theapikey",
       "listId": "thislsitId"
      }
   }
}

chimpMail.js

import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';

const settings = Meteor.settings.private.MailChimp;
const chimp = new MailChimp( settings.apiKey, { version: '2.0' });
const listId = settings.listId;

Meteor.methods({
  'handleSubscriber'(subscriber) {
    check(subscriber, {
      email: String,
      action: String,
    });
  try {
    const subscribe = chimp.call('lists', subscriber.action, {
      id: listId,
       email: {
        email: subscriber.email,
       },
    });
     return subscribe;
    } catch (exception) {
      return exception;
   }
 },
});

这是我的 emailSubscription.jsx

import { Meteor } from 'meteor/meteor';
import React, { Component } from 'react';
import { createContainer } from 'meteor/react-meteor-data';

class EmailSubscription extends Component {
   handleOnSubmit(event) {
     event.preventDefault();

     Meteor.call('handleSubscriber', this.props.subscriber,        
       function(error, response) {
       if (error) {
          Bert.alert(error.reason, "warning");
       } else {
       if (response.complete || response.euid) {
          const subscribeMessage = 'Please confirm your email to  
           complete your subscription!';
          const unsubscribeMessage = subscriber.email + 'successfully 
           unsubscribed!';
          const message = subscriber.action === "subscribe" ? 
           subscribeMessage : unsubscribeMessage;

          Bert.alert(message, "success");
       } else {
         Bert.alert(response.message, "warning");
       }
     }
   });
}

render() {
  return (
    <div className="container">
      <form onSubmit={this.handleOnSubmit.bind(this)} 
         className="emal-submit"
       >
        <input type="email" placeholder="email@example.com" />
        <button>Sign Me Up!</button>
      </form>
    </div>
   );
  }
}
export default createContainer(() => {
}, EmailSubscription);

【问题讨论】:

  • "method handlerSubscriber not found" 是说你的方法。请在服务器目录中导入方法文件。
  • 感谢您的评论,我忘了!我将 mailChimp.js 导入到我的服务器目录中,但出现以下错误:` 调用方法 'handleSubscriber' 时出现异常错误:匹配错误:预期对象,得到 null`

标签: javascript meteor reactjs mailchimp


【解决方案1】:

“method handlerSubscriber not found”是说你的方法。

请导入服务器目录下的方法文件。

调用方法“handleSubscriber”时出现异常错误:匹配错误:预期对象,得到空

这是您遇到的异常,因为订阅者需要是一个对象,但您从客户端发送 null。检查抛出此异常的函数。

【讨论】:

  • 谢谢,我刚刚解决了这个问题,现在可以了!我会用答案更新帖子!!!
猜你喜欢
  • 2016-05-09
  • 2021-04-02
  • 2017-04-29
  • 2016-05-09
  • 1970-01-01
  • 2020-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多