SyntaxError:JSON中出现意外的令牌#

时间:2018-04-30 00:18:06

标签: node.js angular http

所以我得到了这个错误,显然每个其他的noob角度dev都会得到。尝试在登录页面上进行一些测试,我将电子邮件注册到我的数据库,但每次都会出现此语法错误。

错误

import { Component, OnInit } from '@angular/core';
import { ValidateService } from '../../services/validate.service';
import { AuthService } from '../../services/auth.service'

@Component({
  selector: 'app-landing',
  templateUrl: './landing.component.html',
  styleUrls: ['./landing.component.css']
})
export class LandingComponent implements OnInit {
  email: String;

  constructor(
    private validateService: ValidateService,
    private authService: AuthService
  ) { }

  ngOnInit() {
  }

  onSubmit(){
    console.log(this.email)
    if(!this.validateService.validateEmail(this.email)){
      console.log("please use valid email")
      return false
    } else {
      this.authService.registerEmail(this.email).subscribe(data =>{
        if(data.success){
          console.log('you are now registered')
        } else {
          console.log('you are not registered')
        }
      })
    }
  }
}

landing.component.ts

import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import 'rxjs/add/operator/map'

@Injectable()
export class AuthService {
  authToken: any;
  userEmail: any;

  constructor(
    private http: Http
  ) { }

  registerEmail(userEmail){
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    let opts = {headers: headers}
    return this.http.post('http://localhost:3000/users/notifyme', userEmail, opts)
      .map(res => res.json())
  }

}

auth.service.ts

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

const User = require('../models/users')

router.post('/notifyme', (req, res, next) =>{
  let newUser = new User({
    email: req.body.email,
  })

  User.addEmail(newUser, (err, user)=>{
    if(err){
      res.json({success:false, msg: "failed to register"})
    } else {
      res.json({sucess:true, msg: "registered email"})
    }
  })})


module.exports = router;

用户/通知路由

addEmail

然后Sheet1.ColA函数只是将电子邮件保存到数据库。

0 个答案:

没有答案