Я пытаюсь создать документ в моей базе данных Cloud Firestore, используя следующий код:
Backend:
index.js:
const express = require('express');
const chatrooms = express();
chatrooms.use(cors);
let chatroomsEndpoint = require('./chatroomsEndpoint')
chatrooms.post('/', chatroomsEndpoint.createChatroom); // Create new chatroom
exports.chatrooms = functions.https.onRequest(chatrooms);
chatroomsEndpoint.js:
const db = admin.firestore();
const chatroomsCollection = db.collection("chatrooms");
exports.createChatroom = function (req, res) {
chatroomsCollection.add(req.body)
.then(resul => {
console.log('Successfully added chatroom');
return res.status(201).send(true);
})
.catch((error) => {
console.log('Error creating new chatroom', error)
return res.send(false);
})
}
Frontend:
chat.service.ts:
import { HttpClient } from '@angular/common/http';
constructor(
private authService: AuthService,
private http: HttpClient,
private router: Router
) {
this.headers.append('Content-Type', 'application/json');
}
async create() {
const userA = 'FeuzErzVIfTXKVvuagBzLvGcETK2';
const userB = 'jmYZlOIjQkbOPnlVt54cfsdvJFb2';
const data = {
userA,
userB,
createdAt: Date.now(),
messages: []
};
await this.http.put(`${this.apiUrl}/chatrooms`, data);
return this.router.navigate([`chats/XvlQIXo650VFhjMsOlf5`]) && console.log('return is executed');
}
Chatroom-list.component.html:
<button class="button is-info" (click)="chatService.create()">Create New Chat</button>
Моя проблема в том, что после нажатия кнопки «Создать новый чат» я просто получаю console.log («возврат выполнен») и перехожу к «chats / XvlQIXo650VFhjMsOlf5», но в Firestore и моих функциях документ не создается не срабатывай.
Также нет ошибок на стороне клиента и ошибок на консоли Firebase (в разделе «Функции / Журналы»). Я просто хочу попробовать создать документ с маршрутом. Я пытался найти помощь в других сообщениях, но я даже не уверен, какая у меня проблема.