【发布时间】:2018-03-14 19:32:15
【问题描述】:
我是 Go 和 Echo 的初学者。 我需要存储一个 html 模板(电子邮件模板),它还将包含一些作为上下文传递的详细信息。以便它可以存储到正文列(MySQL 中的文本)并稍后触发。
if user.Email !=""{
visitingDetails := H{"user_name" : user.Fname,
"location" : location.Name,
"visitor_company": visitor.Company,
"visitor_name" : visitor.Fname +" "+visitor.Lname,
"visitor_phone" : visitor.Phone,
"visitor_email" : visitor.Email,
"visitor_fname" : visitor.Fname,
"visitor_image" : visitor.ProfilePicture,
}
subject := visitor.Fname +" has come to visit you at the reception"
body := c.Render(http.StatusOK,"email/user_notify_email.html",visitingDetails)
emailJob := models.EmailJob{Recipients: visitor.Email , Subject: subject, Body: body}
db.Create(&emailJob)
if db.NewRecord(emailJob){
fmt.Println("Unable to send email")
}
}
电子邮件工作
type EmailJob struct {
Id int
Recipients string
Subject string
Body string
Sent int
Error string
}
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}
body := c.Render(http.StatusOK,"email/user_notify_email.html",visitingDetails)
这一行给出了错误,因为它返回了渲染错误。 我不知道我会怎么做?我希望我说清楚了。非常感谢您的帮助。
【问题讨论】:
-
错误是什么?
-
app/controllers/controllers.go:733:91: cannot use body (type error) as type string in field value ...这是我得到的错误。 @CeriseLimón
-
您看错了行。错误出现在 emailJob 分配中。什么是c?大概 c.Render 返回一个错误,而不是渲染的模板。
-
C 是 c echo.Context 。并且 Render func 已经在细节中提到了。是的,我认为你是对的,@Peter。它可能会执行模板。