Cannot use notifs (variable of type []entity.Notif) as string value in array or slice literal

huangapple go评论107阅读模式
英文:

Cannot use notifs (variable of type []entity.Notif) as string value in array or slice literal

问题

我想从数据库中获取电子邮件数据,然后使用该数据发送电子邮件。

这是来自controller/notif-controller.go的代码:

  1. func (c *notifController) SendNotifEmail(context *gin.Context) {
  2. email_to := context.Query("sEmailByDiv")
  3. cc_to := context.Query("cc_to")
  4. subject := context.Query("subject")
  5. body := context.Query("body")
  6. notifs := c.notifService.EmailByDiv(email_to)
  7. to := []string{notifs}
  8. mailer := gomail.NewMessage()
  9. mailer.SetHeader("From", CONFIG_SENDER_NAME)
  10. mailer.SetHeader("To", to)
  11. mailer.SetHeader("Cc", cc_to)
  12. mailer.SetHeader("Subject", subject)
  13. mailer.SetBody("text/html", body)
  14. // dialer := &gomail.Dialer{Host: CONFIG_SMTP_HOST, Port: CONFIG_SMTP_PORT}
  15. dialer := gomail.NewDialer(
  16. CONFIG_SMTP_HOST,
  17. CONFIG_SMTP_PORT,
  18. CONFIG_AUTH_EMAIL,
  19. CONFIG_AUTH_PASSWORD,
  20. )
  21. err := dialer.DialAndSend(mailer)
  22. if err != nil {
  23. log.Fatal(err.Error())
  24. }
  25. log.Println("Mail sent!")
  26. }

我在以下位置遇到错误:

无法将类型为[]entity.Notif的变量notifs用作数组或切片字面值中的字符串值,也无法将类型为[]string的变量to用作mailer.SetHeader的参数中的字符串值。

我添加了一个循环,代码如下:

  1. func (c *notifController) SendNotifEmail(context *gin.Context) {
  2. email_to := context.Query("sEmailByDiv")
  3. cc_to := context.Query("cc_to")
  4. subject := context.Query("subject")
  5. body := context.Query("body")
  6. // file := context.Query("file")
  7. notifs := c.notifService.EmailByDiv(email_to)
  8. to := []string{notifs}
  9. mailer := gomail.NewMessage()
  10. addresses := make([]string, len(to))
  11. for i, recipient := range to {
  12. addresses[i] = mailer.FormatAddress(recipient, "")
  13. }
  14. mailer.SetHeader("From", CONFIG_SENDER_NAME)
  15. mailer.SetHeader("To", addresses...)
  16. mailer.SetHeader("Cc", cc_to)
  17. mailer.SetHeader("Subject", subject)
  18. mailer.SetBody("text/html", body)
  19. // mailer.Attach(file)
  20. // dialer := &gomail.Dialer{Host: CONFIG_SMTP_HOST, Port: CONFIG_SMTP_PORT}
  21. dialer := gomail.NewDialer(
  22. CONFIG_SMTP_HOST,
  23. CONFIG_SMTP_PORT,
  24. CONFIG_AUTH_EMAIL,
  25. CONFIG_AUTH_PASSWORD,
  26. )
  27. err := dialer.DialAndSend(mailer)
  28. if err != nil {
  29. log.Fatal(err.Error())
  30. }
  31. log.Println("Mail sent!")
  32. }

我遇到了以下错误:

  1. 无法将类型为`[]entity.Notif`的变量`notifs`用作数组或切片字面值中的字符串值

entity/notif.go中的entity.Notif结构体中包含以下内容:

  1. package entity
  2. type Notif struct {
  3. Email string `gorm:"type:varchar(255)" json:"email"`
  4. }
英文:

I want to fetch email data from database to send email with that data.

This code from controller/notif-controller.go:

  1. func (c *notifController) SendNotifEmail(context *gin.Context) {
  2. email_to := context.Query("sEmailByDiv")
  3. cc_to := context.Query("cc_to")
  4. subject := context.Query("subject")
  5. body := context.Query("body")
  6. notifs := c.notifService.EmailByDiv(email_to)
  7. to := []string{notifs}
  8. mailer := gomail.NewMessage()
  9. mailer.SetHeader("From", CONFIG_SENDER_NAME)
  10. mailer.SetHeader("To", to)
  11. mailer.SetHeader("Cc", cc_to)
  12. mailer.SetHeader("Subject", subject)
  13. mailer.SetBody("text/html", body)
  14. // dialer := &gomail.Dialer{Host: CONFIG_SMTP_HOST, Port: CONFIG_SMTP_PORT}
  15. dialer := gomail.NewDialer(
  16. CONFIG_SMTP_HOST,
  17. CONFIG_SMTP_PORT,
  18. CONFIG_AUTH_EMAIL,
  19. CONFIG_AUTH_PASSWORD,
  20. )
  21. err := dialer.DialAndSend(mailer)
  22. if err != nil {
  23. log.Fatal(err.Error())
  24. }
  25. log.Println("Mail sent!")
  26. }

I have Error in :

cannot use notifs (variable of type []entity.Notif) as string value in array or slice literal
and cannot use to (variable of type []string) as string value in argument to mailer.SetHeader.

I've added a loop and it's like this :

  1. func (c *notifController) SendNotifEmail(context *gin.Context) {
  2. email_to := context.Query("sEmailByDiv")
  3. cc_to := context.Query("cc_to")
  4. subject := context.Query("subject")
  5. body := context.Query("body")
  6. // file := context.Query("file")
  7. notifs := c.notifService.EmailByDiv(email_to)
  8. to := []string{notifs}
  9. mailer := gomail.NewMessage()
  10. addresses := make([]string, len(to))
  11. for i, recipient := range to {
  12. addresses[i] = mailer.FormatAddress(recipient, "")
  13. }
  14. mailer.SetHeader("From", CONFIG_SENDER_NAME)
  15. mailer.SetHeader("To", addresses...)
  16. mailer.SetHeader("Cc", cc_to)
  17. mailer.SetHeader("Subject", subject)
  18. mailer.SetBody("text/html", body)
  19. // mailer.Attach(file)
  20. // dialer := &gomail.Dialer{Host: CONFIG_SMTP_HOST, Port: CONFIG_SMTP_PORT}
  21. dialer := gomail.NewDialer(
  22. CONFIG_SMTP_HOST,
  23. CONFIG_SMTP_PORT,
  24. CONFIG_AUTH_EMAIL,
  25. CONFIG_AUTH_PASSWORD,
  26. )
  27. err := dialer.DialAndSend(mailer)
  28. if err != nil {
  29. log.Fatal(err.Error())
  30. }
  31. log.Println("Mail sent!")
  32. }

and I have an error like this :

  1. cannot use notifs (variable of type []entity.Notif) as string value in array or slice literal

This file for entity.Notif from entity/notif.go contains:

  1. package entity
  2. type Notif struct {
  3. Email string `gorm:"type:varchar(255)" json:"email"`
  4. }

答案1

得分: 0

使用循环从notifs中提取电子邮件地址作为字符串切片:

  1. addresses := make([]string, len(notifs))
  2. for i, notif := range notifs {
  3. addresses[i] = notif.Email
  4. }

使用该切片设置邮件头:

  1. mailer.SetHeader("To", addresses...)
英文:

Write a loop to extract the email addresses from notifs as a slice of strings:

  1. addresses := make([]string, len(notifs))
  2. for i, notif := range notifs {
  3. addresses[i] = notif.Email
  4. }

Set the header using the slice:

  1. mailer.SetHeader("To", addresses...)

huangapple
  • 本文由 发表于 2021年10月7日 12:24:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/69475441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定