I want to add new document to the mongodb and get an inserted id as response with 201 status, but when db.insertOne called I get response with 204 status code before tap works. Here is my controller
@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();
}
}
In my 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');
}
In 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;
}));
}
How can I get json response with correct status code and body