node.js - 使用 Angular 8 在 MongoDB 中保存新条目时出现 header 错误

标签 node.js angular express mean-stack angular8

我正在尝试使用 Angular 在 MongoDB 中添加新条目。我无法摆脱以下错误消息。有任何想法吗?先感谢您!

(注意:与数据库的连接正常。例如,我的 get() 函数正在运行)

错误消息

C:\Server Files\112-IT-Dep-Equipment-Registering (Back-end)\node_modules\mongodb\lib\utils.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 Files\112-IT-Dep-Equipment-Registering (Back-end)\node_modules\express\lib\response.js:725:10)
    at ServerResponse.send (C:\Server Files\112-IT-Dep-Equipment-Registering (Back-end)\node_modules\express\lib\response.js:170:12)
    at ServerResponse.json (C:\Server Files\112-IT-Dep-Equipment-Registering (Back-end)\node_modules\express\lib\response.js:256:15)
    at db.get.collection.insert (C:\Server Files\112-IT-Dep-Equipment-Registering (Back-end)\routes\api.js:22:21)
    at C:\Server Files\112-IT-Dep-Equipment-Registering (Back-end)\node_modules\mongodb\lib\collection.js:525:20
    at C:\Server Files\112-IT-Dep-Equipment-Registering (Back-end)\node_modules\mongodb\lib\collection.js:659:14
    at handleCallback (C:\Server Files\112-IT-Dep-Equipment-Registering (Back-end)\node_modules\mongodb\lib\utils.js:120:56)
    at resultHandler (C:\Server Files\112-IT-Dep-Equipment-Registering (Back-end)\node_modules\mongodb\lib\bulk\ordered.js:421:14)
    at C:\Server Files\112-IT-Dep-Equipment-Registering (Back-end)\node_modules\mongodb-core\lib\connection\pool.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;
            });
    }

最佳答案

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

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'});
            });
});

关于node.js - 使用 Angular 8 在 MongoDB 中保存新条目时出现 header 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58980556/

相关文章:

angular - ionic 3 导航菜单不显示

node.js - 如何在 node.js 中查找经纬度?

javascript - TypeError : Router. use() 需要中间件函数,但在 Function.use 处未定义

node.js - 如何设置端点来处理nginx后面的express中的websockets

javascript - 为什么要安装 Browserify 两次才能捆绑

node.js - 如何丑化文件并保存到另一个位置(vue.js)

node.js - 在 Express(和 Grunt)中使用 i18next 时,Jade 不会编译为 Html

javascript - graphQL 解析器不等待 Web API 完成

javascript - 如何通过点击按钮使元素为 `display: none`

angular - 将数据从服务传递到组件 --> 子组件