Angular res.cookie有问题,但res.json没有问题

Angular res.cookie有问题,但res.json没有问题,angular,express,cookies,Angular,Express,Cookies,当我在ExpressPOST路由响应中使用res.json()时,一切正常。当我将响应更改为res.cookie()时,我遇到了MongoDB 11000 Dup Key Error,并出现以下奇怪行为: 文档正确地保存到MongoDB,但是POSTXHR调用似乎有问题,因为浏览器控制台显示POST…-ms,而通常它以毫秒为单位显示一个数字,即POST-32毫秒(即使它仍然显示200code) 过了一会儿,在一段延迟之后,我得到了Dup Key Error,因为路由似乎试图再次保存文档 我在前端

当我在Express
POST
路由响应中使用
res.json()
时,一切正常。当我将响应更改为
res.cookie()
时,我遇到了
MongoDB 11000 Dup Key Error
,并出现以下奇怪行为:

  • 文档正确地保存到MongoDB,但是
    POST
    XHR调用似乎有问题,因为浏览器控制台显示
    POST…-ms
    ,而通常它以毫秒为单位显示一个数字,即
    POST-32毫秒(即使它仍然显示
    200
    code)
  • 过了一会儿,在一段延迟之后,我得到了
    Dup Key Error
    ,因为路由似乎试图再次保存文档 我在前端或后端都没有其他控制台错误,根据控制台日志,cookie的内容似乎还可以

    你知道会发生什么吗

    模板

    <form #f="ngForm" [formGroup]="userForm" (ngSubmit)="doSignUp(f)">
        <ng-container *ngFor="let key of keyArr; index as i"> 
    
        <label [hidden]='key[1]'> {{key[2]}}
          <div>
              <input *ngIf="key[1]==='password' type='password' [formControlName]='key[0]' id={{key[0]}} name={{key[0]}}/>
              <input *ngIf="key[1]!=='password' type='text' [formControlName]='key[0]' id={{key[0]}} name={{key[0]}}/>
          </div>
         </label>
        <br/>
    
        </ng-container>
        <button>Submit</button>
    
    </form>
    
    postSignupForm(f: NgForm): Observable<any> {
        return this.http.post(this.signup_url, f.value);
    }
    
    UserSchema.methods.generateJWT = function() {
        const today = new Date();
        const expirationDate = new Date(today);
        expirationDate.setDate(today.getDate() + 60);
    
        const thisJwt = jwt.sign({
            email: this["authData"].mainEmail.value[this["authData"].mainEmail.value.length-1],
            id: this._id,
            exp: parseInt(expirationDate.getTime() / 1000, 10),
        }, 'secret');
        console.log(`TOKEN: ${JSON.stringify(thisJwt)}`);
        return thisJwt;
    }
    
    认证服务(会员服务)

    COOKIE中间件

    <form #f="ngForm" [formGroup]="userForm" (ngSubmit)="doSignUp(f)">
        <ng-container *ngFor="let key of keyArr; index as i"> 
    
        <label [hidden]='key[1]'> {{key[2]}}
          <div>
              <input *ngIf="key[1]==='password' type='password' [formControlName]='key[0]' id={{key[0]}} name={{key[0]}}/>
              <input *ngIf="key[1]!=='password' type='text' [formControlName]='key[0]' id={{key[0]}} name={{key[0]}}/>
          </div>
         </label>
        <br/>
    
        </ng-container>
        <button>Submit</button>
    
    </form>
    
    postSignupForm(f: NgForm): Observable<any> {
        return this.http.post(this.signup_url, f.value);
    }
    
    UserSchema.methods.generateJWT = function() {
        const today = new Date();
        const expirationDate = new Date(today);
        expirationDate.setDate(today.getDate() + 60);
    
        const thisJwt = jwt.sign({
            email: this["authData"].mainEmail.value[this["authData"].mainEmail.value.length-1],
            id: this._id,
            exp: parseInt(expirationDate.getTime() / 1000, 10),
        }, 'secret');
        console.log(`TOKEN: ${JSON.stringify(thisJwt)}`);
        return thisJwt;
    }
    

    有什么想法吗?谢谢

    在后端,在res.cookie()之后,我需要添加res.send('done')

    在前端,我需要将
    responseType
    添加到
    POST
    调用中:

    postSignupForm(f: NgForm): Observable<any> {
        return this.http.post(this.signup_url, f.value, {responseType: 'text'});
    }
    
    postSignupForm(f:NgForm):可观察{
    返回this.http.post(this.signup_url,f.value,{responseType:'text'});
    }