node.js - 使用 Angular 8 在 MongoDB 中保存新条目时出现标题错误
问题描述
我正在尝试使用 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;
}
入口服务.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'});
});
});
推荐阅读
- ios - 视图是黑色的,当返回到导航控制器堆栈视图时,在实现自定义转换后
- assembly - 添加彩色文本(我的自定义 MBR)
- php - 如何在php中验证用户名和密码?
- c++ - Visual Studio Code C++ 删除 c_cpp_properties.json 中的“定义”
- java - jTable中的DefaultTableModel空行
- python - flask-marshmallow:仅当方法为 PUT 时如何将所有字段标记为可选
- jenkins - Jenkins 声明式管道:在不阻塞执行器的情况下使用条件步骤输入
- javascript - Ext JS 应用程序空白页
- assembly - 谁负责在 MIPS 中保存调用保存的寄存器?
- stl - FlannBasedMatcher 没有成员 knnMatch