gpt4 book ai didi

node.js - Firestore 查询 endBefore 和 endAt 未按预期工作

转载 作者:行者123 更新时间:2023-12-03 12:17:46 24 4
gpt4 key购买 nike

我正在使用 Node 10、firebase 8.0、firebase-admin 9.3.0。我已经包含了我的测试代码和客户端 js 版本的副本。
数据

Document ID | name | is_visible
----------- | ---- | ----------
1 | a | true
2 | b | true
3 | c | true
4 | d | true
5 | e | true
6 | f | true
7 | g | true
8 | h | true
9 | i | true
代码
var admin = require("firebase-admin");

const firebaseConfig = {xxx}

admin.initializeApp({
credential: admin.credential.cert(firebaseConfig)
});

const getDocs = async () =>{

try {

const cursor = await admin.firestore().collection('ordering').doc('5').get();

const beforeDocs = await admin.firestore().collection('ordering')
.orderBy("name", "desc")
.endAt(cursor)
.limit(2)
.get()


beforeDocs.forEach((doc)=> {
console.log("Doc ID - ",doc.id, ", Name -", doc.get('name'));
})


const afterDocs = await admin.firestore().collection('ordering')
.orderBy("name", "desc")
.startAt(cursor)
.limit(2)
.get()


afterDocs.forEach((doc)=> {
console.log("Doc ID - ",doc.id, ", Name -", doc.get('name'));
})

}
catch (error) {
console.error(error);
}
}

getDocs();

使用 node getDocs.js 运行
期待结果
Doc ID -  6 , Name - f
Doc ID - 5 , Name - e
Doc ID - 5 , Name - e
Doc ID - 4 , Name - d

实际结果
Doc ID -  9 , Name - i
Doc ID - 8 , Name - h
Doc ID - 5 , Name - e
Doc ID - 4 , Name - d
我对 endAt 的临时解决方法是将顺序反转为“asc”使用 startAt 然后在 QuerySnapshot 中反转 forEach 之前的 docs 数组。
我创建了这个 JS 来显示我的问题,这是我的测试集合的运行,它非常粗糙,请善待,输出到控制台。
我显然做错了什么,但不确定是什么。

<script src="https://www.gstatic.com/firebasejs/8.0.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.0.0/firebase-firestore.js"></script>

<body>
<div id="output">Check the console!</div>
</body>
<script>

var firebaseConfig = {
apiKey: "AIzaSyAxLEo7LfABdATToJTMQGae3Vgum_CAlQg",
authDomain: "bmtestbase.firebaseapp.com",
databaseURL: "https://bmtestbase.firebaseio.com",
projectId: "bmtestbase",
storageBucket: "bmtestbase.appspot.com",
messagingSenderId: "326028950684",
appId: "1:326028950684:web:74b55887466c63b4a9c159"
};

// Initialize Firebase
firebase.initializeApp(firebaseConfig);

const db = firebase.firestore()

// Setting the Focus point using a Snapshot
db.collection('ordering').get()
.then (docs => {
docs.forEach(doc => {
console.log("MYCODE_allDocs: DocID ", doc.id, ", Name ", doc.get('name'))
})
})

db.collection('ordering').doc('5').get()
.then( focusSnapshot => {

db.collection("ordering")
.orderBy("name", "asc")
.endAt(focusSnapshot)
.limit(2)
.get()
.then(docs => {

console.log("MYCODE_endAt_expected:","DocID 4, Name d")
console.log("MYCODE_endAt_expected:","DocID 5, Name e")

docs.forEach(doc => {
console.log("MYCODE_endAt:","DocID ", doc.id, ", Name ", doc.get('name'))
});
})


db.collection("ordering")
.orderBy("name", "asc")
.startAt(focusSnapshot)
.limit(2)
.get()
.then(docs => {

console.log("MYCODE_startAt_expected:","DocID 5, Name e")
console.log("MYCODE_startAt_expected:","DocID 6, Name f")

docs.forEach(doc => {
console.log("MYCODE_startAt:","DocID ", doc.id, ", Name ", doc.get('name'))
});
})

})

</script>

最佳答案

根据firebase支持,您需要使用limitToLastendBeforeendAt
像这样重写你的例子应该有效:

<script src="https://www.gstatic.com/firebasejs/8.0.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.0.0/firebase-firestore.js"></script>

<body>
<div id="output">Check the console!</div>
</body>
<script>

var firebaseConfig = {
apiKey: "AIzaSyAxLEo7LfABdATToJTMQGae3Vgum_CAlQg",
authDomain: "bmtestbase.firebaseapp.com",
databaseURL: "https://bmtestbase.firebaseio.com",
projectId: "bmtestbase",
storageBucket: "bmtestbase.appspot.com",
messagingSenderId: "326028950684",
appId: "1:326028950684:web:74b55887466c63b4a9c159"
};

// Initialize Firebase
firebase.initializeApp(firebaseConfig);

const db = firebase.firestore()

// Setting the Focus point using a Snapshot
db.collection('ordering').get()
.then (docs => {
docs.forEach(doc => {
console.log("MYCODE_allDocs: DocID ", doc.id, ", Name ", doc.get('name'))
})
})

db.collection('ordering').doc('5').get()
.then( focusSnapshot => {

db.collection("ordering")
.orderBy("name", "asc")
.endAt(focusSnapshot)
.limitToLast(2)
.get()
.then(docs => {

console.log("MYCODE_endAt_expected:","DocID 4, Name d")
console.log("MYCODE_endAt_expected:","DocID 5, Name e")

docs.forEach(doc => {
console.log("MYCODE_endAt:","DocID ", doc.id, ", Name ", doc.get('name'))
});
})


db.collection("ordering")
.orderBy("name", "asc")
.startAt(focusSnapshot)
.limit(2)
.get()
.then(docs => {

console.log("MYCODE_startAt_expected:","DocID 5, Name e")
console.log("MYCODE_startAt_expected:","DocID 6, Name f")

docs.forEach(doc => {
console.log("MYCODE_startAt:","DocID ", doc.id, ", Name ", doc.get('name'))
});
})

})

</script>

关于node.js - Firestore 查询 endBefore 和 endAt 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64666057/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com