Получение 204 ответа до обратного вызова действия db в expressjs, mongodb, inversifyjs
Я хочу добавить новый документ в mongodb и получить вставленный идентификатор в качестве ответа со статусом 201, но когда вызывается db.insertOne, я получаю ответ с кодом состояния 204 до того, как сработает тап. Вот мой контроллер
@controller('/stars')
export class StarController {
constructor(@inject(TYPES.StarService) private starService: IStarService) {}
@httpPost('/')
public newStar(request: Request, response: Response) {
this.starService.insert(request.body)
.pipe(
tap(result => response.status(201).json(result))
).subscribe();
}
}
В моем StarService
@injectable()
export class StarService implements IStarService {
private mongoClient: MongoDBClient;
constructor(@inject(TYPES.MongoDBClient) mongoClient: MongoDBClient) {
this.mongoClient = mongoClient;
}
public insert(star) {
return this.mongoClient.insertOne(star, 'stars');
}
В MongoDBClient
public insertOne<T>(document: T, collectionName: string): Observable<ObjectID> {
return from(
this.db.collection(collectionName).insertOne(document))
.pipe(
map(
(result: InsertOneWriteOpResult) => {
return result.insertedId;
}));
}
Как я могу получить ответ JSON с правильным кодом состояния и телом