如何从云 Firestore 中提取非重复文档?
How to pull non-duplicate document from cloud firestore?
所以目前这些代码是用来提取我存储在数据库中的文档的,我想添加条件来提取某些文档,但不是重复的。
如何编辑代码,使提取的文档不会已经被提取?
我尝试使用 while 循环,但在这两行出现了这个错误
.then(doc => {
还有
}).catch(err => {
let qnumber = Math.floor((Math.random() * 3) + 1);
agent.add('Question No: ' + count);
count += 1;
const dialogflowAgentDoc = db.collection('esequiz').doc(''+qnumber);
return dialogflowAgentDoc.get()
.then(doc => {
if (!doc.exists) {
agent.add('Question '+qnumber+' not available');
} else {
// agent.add('Quiz Question '+ qnumber);
agent.add(doc.data().question);
let answer = Math.floor(Math.random() * 2) + 1;
if(answer == 1){
agent.add(`1. ` + doc.data().right);
agent.add(`2. ` + doc.data().wrong);
}
else{
agent.add(`1. ` + doc.data().wrong);
agent.add(`2. ` + doc.data().right);
}
agent.add("Which is correct, 1 or 2?");
agent.context.set({
name:'quizanswer',
lifespan: 3,
parameters:{
qnumber:qnumber,
right:answer,
count:count
}
});
}
return Promise.resolve('Read complete');
}).catch(err => {
agent.add('Error reading entry from the Firestore database.');
agent.add('Error: '+ err);
});
There isn't any API to retrieve unique values from Firestore. But you can create a unique id by merging two unique item in your model like
and fetch record on it's basis.
extraParm = userID + anyOtherUniqueID
https://medium.com/@jqualls/firebase-firestore-unique-constraints-d0673b7a4952
所以目前这些代码是用来提取我存储在数据库中的文档的,我想添加条件来提取某些文档,但不是重复的。
如何编辑代码,使提取的文档不会已经被提取?
我尝试使用 while 循环,但在这两行出现了这个错误
.then(doc => {
还有
}).catch(err => {
let qnumber = Math.floor((Math.random() * 3) + 1);
agent.add('Question No: ' + count);
count += 1;
const dialogflowAgentDoc = db.collection('esequiz').doc(''+qnumber);
return dialogflowAgentDoc.get()
.then(doc => {
if (!doc.exists) {
agent.add('Question '+qnumber+' not available');
} else {
// agent.add('Quiz Question '+ qnumber);
agent.add(doc.data().question);
let answer = Math.floor(Math.random() * 2) + 1;
if(answer == 1){
agent.add(`1. ` + doc.data().right);
agent.add(`2. ` + doc.data().wrong);
}
else{
agent.add(`1. ` + doc.data().wrong);
agent.add(`2. ` + doc.data().right);
}
agent.add("Which is correct, 1 or 2?");
agent.context.set({
name:'quizanswer',
lifespan: 3,
parameters:{
qnumber:qnumber,
right:answer,
count:count
}
});
}
return Promise.resolve('Read complete');
}).catch(err => {
agent.add('Error reading entry from the Firestore database.');
agent.add('Error: '+ err);
});
There isn't any API to retrieve unique values from Firestore. But you can create a unique id by merging two unique item in your model like and fetch record on it's basis.
extraParm = userID + anyOtherUniqueID
https://medium.com/@jqualls/firebase-firestore-unique-constraints-d0673b7a4952