问题描述
我正在尝试通过调整 https://mailchimp.com/developer/transactional/api/messages/send-new-message/ 中的 curl 命令并使用 https://us1.admin.mailchimp.com/account/api/ 中的 API 密钥,使用 Mandrill 发送电子邮件,但我收到了一个不会产生很多错误的错误对我有意义。这是我正在运行的命令:
curl -X POST \
https://mandrillapp.com/api/1.0/messages/send \
-d '{"key":"...","message":{"html":"hello,world!","subject":"hello,"from_email":"neubert@neubert.com","to":["neubert@neubert.com"]}}'
回复如下:
{"status":"error","code":-2,"name":"ValidationError","message":"Validation error: {\"message\":{\"to\":[\"Please enter an array\"]}}"}
这里是解码:
{
"status": "error","code": -2,"name": "ValidationError","message": "Validation error: {\"message\":{\"to\":[\"Please enter an array\"]}}"
}
我不明白。 “to”是一个数组...
有什么想法吗?
谢谢!
解决方法
一开始我也遇到了同样的错误,然后敏锐地查看了 documentation,我注意到 constructor(props) {
super(props);
this.state = {
name: "",email: "",role: "",phone: "",errors: {}
}
}
// Initializer: Copy contact's current data to state
componentDidMount() {
axios.get("/api/contacts/get/" + this.props.match.params.id)
.then(res => {
this.setState({ name: res.data.name });
this.setState({ email: res.data.email });
this.setState({ role: res.data.role });
this.setState({ phone: res.data.phone });
})
.catch(err => console.log(err));
this.forceUpdate();
}
// Change handler: Modify values when changed
onChange = e => {
this.setState({ [e.target.id]: e.target.value });
}
// Submit handler: Save changes to database
onSubmit = e => {
e.preventDefault();
const contact = {
name: this.state.name,email: this.state.email,role: this.state.role,phone: stripNumber(this.state.phone)
};
// Post modified contact to database,then navigate back to Manage Contacts Dashboard
axios.post("/api/admin/contacts/update/" + this.props.match.params.id,contact);
window.location = PREVIOUS_URL;
}
render() {
const { errors } = this.state;
return (
<div>
<Link to={PREVIOUS_URL} className="btn-flat waves-effect">
<i className="material-icons left">keyboard_backspace</i> Back to Manage Contacts
</Link>
<h3>Edit contact</h3>
<form noValidate onSubmit={this.onSubmit}>
<div className="input-field col s12">
<input
onChange={this.onChange}
value={this.state.name}
error={errors.name}
id="name"
type="text"
className={classnames("",{ invalid: errors.name })}
/>
<label htmlFor="name">Name</label>
<span className="red-text">{errors.name}</span>
</div>
<div className="input-field col s12">
<input
onChange={this.onChange}
value={this.state.email}
error={errors.email}
id="email"
type="email"
className={classnames("",{ invalid: errors.email || errors.emailnotfound })}
/>
<label htmlFor="email">Email</label>
<span className="red-text">{errors.email}{errors.emailnotfound}</span>
</div>
<div className="input-field col s12">
<input
onChange={this.onChange}
value={this.state.role}
error={errors.role}
id="role"
type="text"
className={classnames("",{ invalid: errors.role })}
/>
<label htmlFor="role">Role</label>
<span className="red-text">{errors.role}</span>
</div>
<div className="input-field col s12">
<input
onChange={this.onChange}
value={this.state.phone}
error={errors.phone}
id="phone"
type="tel"
className={classnames("",{ invalid: errors.phone })}
/>
<label htmlFor="phone">Phone Number</label>
<span className="red-text">{errors.phone}</span>
</div>
<div className="form-group">
<input type="submit" value="Submit changes" className="btn btn-primary" />
<Link to={PREVIOUS_URL} className="btn btn-danger"> Cancel </Link>
</div>
</form>
</div>
)
}
}
中的 to
字段NOT电子邮件地址。我知道这样很直观!!
这是一个包含 3 个字段的对象列表:电子邮件、名称和类型。
理想的 JSON Body 如下:
message