使用 Angular 8 在 MongoDB 中保存新条目时出现标头错误



我正在尝试使用Angular在我的MongoDB中添加一个新条目。我无法摆脱以下错误消息。有什么想法吗?提前谢谢你!

(注意:与数据库的连接是正常工作的。例如,我的 get(( 函数正在工作(

错误信息

C:Server Files112-IT-Dep-Equipment-Registering (Back-end)node_modulesmongodblibutils.js:123
    process.nextTick(function() { throw err; });
                                  ^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:470:11)
    at ServerResponse.header (C:Server Files112-IT-Dep-Equipment-Registering (Back-end)node_modulesexpresslibresponse.js:725:10)
    at ServerResponse.send (C:Server Files112-IT-Dep-Equipment-Registering (Back-end)node_modulesexpresslibresponse.js:170:12)
    at ServerResponse.json (C:Server Files112-IT-Dep-Equipment-Registering (Back-end)node_modulesexpresslibresponse.js:256:15)
    at db.get.collection.insert (C:Server Files112-IT-Dep-Equipment-Registering (Back-end)routesapi.js:22:21)
    at C:Server Files112-IT-Dep-Equipment-Registering (Back-end)node_modulesmongodblibcollection.js:525:20
    at C:Server Files112-IT-Dep-Equipment-Registering (Back-end)node_modulesmongodblibcollection.js:659:14
    at handleCallback (C:Server Files112-IT-Dep-Equipment-Registering (Back-end)node_modulesmongodblibutils.js:120:56)
    at resultHandler (C:Server Files112-IT-Dep-Equipment-Registering (Back-end)node_modulesmongodblibbulkordered.js:421:14)
    at C:Server Files112-IT-Dep-Equipment-Registering (Back-end)node_modulesmongodb-corelibconnectionpool.js:461:18

服务器中的 API

//----- Add a new entry -----
router.post('/addNewEntry', (req, res, next) => {
    var newEntry = req.body;
    db.get().collection('data')
            .insert(newEntry, (err, data) => {
                if (err) { res.send(err) }
                res.json({status: 'OK'});
            });
});

"入门"类

export class Entry {
    _id: string;
    equipment: string;
    model: string;
    serial: string;
    network: string;
    ip: string;
    description: string;
    office: string;
}

entry.service.ts

import { Injectable }                                 from '@angular/core';
import { HttpClient, HttpErrorResponse, HttpHeaders } from '@angular/common/http';
import { throwError }                                 from 'rxjs';
import { catchError, retry }                          from 'rxjs/operators';
import { Entry }                                      from '../Classes/entry';
const httpOptions = {
  headers: new HttpHeaders({
    'Content-Type':  'application/json'
  })
};
@Injectable()
export class EntryService {
    constructor(private http: HttpClient) { }
    // ----- Add a new entry -----
    addNewEntry(newEntry: Entry) {
        return this.http
                   .post<Entry>('http://192.168.1.5:3000/api/addNewEntry', newEntry, httpOptions)
                   .pipe(catchError(this.handleError));
    }
} // end of Service

app.component.ts

addNewEntry(): void {
        this.entryService
            .addNewEntry(this.newEntry)
            .subscribe(res => {
                this.newEntry = { _id: '', equipment: '', model: '', serial: '', network: '', 
                                  ip: '', description: '', squadron: '', office: '' };
                this.success = true;
            }, err => {
              this.errorMsg = err;
              this.error = true;
            });
    }

此问题的原因是您向客户端发送了两次响应。如果出现错误,则应按如下方式return错误。

router.post('/addNewEntry', (req, res, next) => {
    var newEntry = req.body;
    db.get().collection('data')
            .insert(newEntry, (err, data) => {
                if (err) { 
                    return res.send(err) 
                }
                res.json({status: 'OK'});
            });
});

相关内容

  • 没有找到相关文章

最新更新