提交表单时无法显示flsha消息

时间:2018-08-29 15:36:17

标签: node.js angular typescript express bootstrap-4

我在我的网站上使用的是nodejs,Express和angular 6,我刚刚使用nodemailer创建了联系表格。现在,我想在表单提交后使用即时消息在联系人页面上显示,例如

Data succesfully submitted ! Thanks',反之亦然

这是表格

<h1>Contact us</h1>
 <flash-messages></flash-messages>

  <form [formGroup]="angForm" method="POST" novalidate>
    <div class="message">
      <h3> Write to us </h3>
    </div>
    <div class="form__top">
      <div class="form__left">
        <div class="form__group">
          <input class="form__input form__input--name" type="text"   formControlName="name" placeholder="name" #name>
        </div>
        <div *ngIf="angForm.controls['name'].invalid && (angForm.controls['name'].dirty || angForm.controls['name'].touched)" class="alert alert-danger">
          <div *ngIf="angForm.controls['name'].errors.required">
            Name is required.
          </div>
        </div>
        <div class="form__group">
          <input class="form__input form__input--email" type="email"  formControlName="email" placeholder="email" #email>
        </div>
        <div *ngIf="angForm.controls['email'].invalid && (angForm.controls['message'].dirty || angForm.controls['message'].touched)"
          class="alert alert-danger">
          <div *ngIf="angForm.controls['message'].errors.required">
            message is required.
          </div>
        </div>
      </div>
      <div class="form__right">
        <div class="form__group">
          <textarea class="form__input form__input--textarea" placeholder="Message" formControlName="message"  #message
            rows="3"></textarea>
        </div>
        <div *ngIf="angForm.controls['message'].invalid && (angForm.controls['message'].dirty || angForm.controls['message'].touched)"
          class="alert alert-danger">
          <div *ngIf="angForm.controls['message'].errors.required">
            message is required.
          </div>
        </div>
      </div>
    </div>

    <div class="form__down">
      <div class="form__group">
        <button (click)="sendMail(name.value, email.value, message.value)" [disabled]="angForm.pristine || angForm.invalid"  class="form__input form__input--submit" name="submit" type="submit" value="SEND MESSAGE">SEND MESSAGE
        </button>
      </div>
    </div>
  </form>

这是components.ts

import { Component, OnInit } from '@angular/core';
import { ContactService } from '../../contact.service';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { FlashMessagesModule, FlashMessagesService } from 'angular2-flash-messages';
@Component({
  selector: 'app-footer',
  templateUrl: './footer.component.html',
  styleUrls: ['./footer.component.scss']
})
export class FooterComponent implements OnInit {
  angForm: FormGroup;
  constructor(
    private flashMessages: FlashMessagesService,
    private fb: FormBuilder,
    private contactService: ContactService) {
    this.createForm();
  }

  createForm() {
    this.angForm = this.fb.group({
      name: ['', Validators.required],
      email: ['', Validators.required],
      message: ['', Validators.required],
    });
  }
  sendMail(name, email, message) {
    this.contactService.sendEmail(name, email, message).subscribe(() => {
      this.flashMessages.show('You are data we succesfully submitted', { cssClass: 'alert-success', timeout: 3000 });
    }, error => {
      this.flashMessages.show('Something went wrong', { cssClass: 'alert-danger', timeout: 3000 });
    });
  }
  ngOnInit() {
  }

}

这是contact.js(节点邮件程序设置)

const express = require('express');
const router = express.Router();
const request = require('request');
const nodemailer = require('nodemailer');

router.post('/send', (req, res) => {
    const outputData = `
    <p>You have a new contact request</p>
    <h3>Contact Details</h3>
    <ul>  
      <li>Name: ${req.body.name}</li>
      <li>Email: ${req.body.email}</li>
    </ul>
    <h3>Message</h3>
    <p>${req.body.message}</p>
  `;

    let transporter = nodemailer.createTransport({
       service: 'gmail',
        auth: {
            user: 'myEmail',
            pass: 'pass'
        },
        tls: {
            rejectUnauthorized: false
        }
    });

    let HelperOptions = {
        from: '"name" <myEmail',
        to: 'myEmail',
        subject: 'Majeni Contact Request',
        text: 'Hello',
        html: outputData
    };



    transporter.sendMail(HelperOptions, (error, info) => {
        if (error) {
            return console.log(error);
        }
        console.log("The message was sent!");
        console.log(info);
    });

});
module.exports = router;

现在我可以发送消息了,但是我希望在提交数据时显示成功的消息。不幸的是,什么也没有显示,但是数据已提交。

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

代码看起来完全正常,它应该可以按预期工作,无论如何,如果您将任何api与node.js一起使用,则可以设置超时以如下方式发送响应,

 setTimeout((function() {res.send("ok")}), 2000);