我正在尝试将一个文档的内容复制到另一个集合中,但是我做不到。 这是我的代码
首先我得到参考:
this.pollRef = this.afs.collection('polls').doc(pollId);
然后我尝试使用以下代码将其复制到另一个集合中:
//Option 1 copy the documentRef
var idBefore = this.afs.createId();
console.log(idBefore);
const datas = { name: 'hola' }
this.afs.collection('rooms').doc(idBefore).set(datas);
var idBefore2 = this.afs.createId();
this.afs.collection('rooms')
.doc(idBefore)
.collection('poll')
.doc(idBefore2)
.set(this.pollRef);
它抛出此错误:
错误错误:函数DocumentReference.set()调用无效 数据。数据必须是一个对象,但是它是:一个自定义 AngularFirestoreDocument对象
答案 0 :(得分:2)
最后我可以解决它。我分享社区的答案
var idBefore = this.afs.createId();
console.log(idBefore);
const datas = { name: 'hola' }
this.afs.collection('rooms').doc(idBefore).set(datas);
var pollId: string = this.roomForm.get('pollId').value;
const x = this.pollsService.getPoll(pollId);
x.subscribe(poll => {
//this.spinnerService.hide();
const pollData = poll.payload.data() as Poll;
pollData["id"] = pollId;
this.PollModel = pollData;
console.log(this.PollModel);
this.afs.doc(`rooms/${idBefore}`).collection('polls').add(this.PollModel)
});