【发布时间】:2020-04-04 20:56:50
【问题描述】:
我在这个问题上花了 6 个小时,但仍然无法解决这个问题。我正在尝试使用 firestore 按订单状态查询订单。但是由于某种原因,当我在其中添加 .where 时,该功能不起作用。我试过用其他字段查询,也不行。
exports.getOrderHistory = (req, res) => {
return db
.collection('orders')
.where('orderStatus', '==', "Active") //this line doesnt work
.orderBy('createdAt', 'desc')
.get()
.then((data) =>{
let orders =[];
data.forEach((doc)=>{
orders.push(doc.data()
);
})
return res.json(orders)
})
.catch((err) =>{
console.error(err)
res.status(500).json({error:err.code})
})
}
但是,我已经使用它从“用户”集合中获取身份验证令牌,并且效果很好。
let idToken;
if (
req.headers.authorization &&
req.headers.authorization.startsWith("Bearer ")
) {
idToken = req.headers.authorization.split("Bearer ")[1];
} else {
console.error("No token found");
return res.status(403).json({ error: "Unauthorised" });
}
admin
.auth()
.verifyIdToken(idToken)
.then(decodedToken => {
req.user = decodedToken;
return db
.collection("users")
.where("userId", "==", req.user.uid) // it works fine here
.limit(1)
.get();
})
.then(data => {
req.user.handle = data.docs[0].data().handle;
return next();
})
.catch(err => {
console.err("Error while verifying token", err);
return res.status(403).json(err);
});
};
【问题讨论】:
-
" 当我在其中添加
.where时,该功能不起作用" 什么不起作用? -
我想我会做一些小事来尝试调试。不在“where”中混合单引号和双引号,检查“Active”是否区分大小写
-
当您说不工作时,您是否遇到错误?或者,只是不是您预期的结果?
-
@FrankvanPuffelen 我交叉在 reddit 上发布了我的问题。 u/pagerussel 找到了罪魁祸首,就是 orderBy()。 “orderBy() 子句还过滤给定字段的存在。结果集将不包括不包含给定字段的文档。”看来我的 orderby 字段为空。至于错误,我在 vscode 中没有收到任何错误,只是没有返回我的 api 调用。我用 Postman 试了一下,它返回错误:9(我认为它与授权有关,但我不确定)
-
请提供该问题的链接,以便人们可以在那里阅读答案。或者在这里发布它作为答案,但在这种情况下,请仍然链接到 reddit 帖子。
标签: reactjs firebase google-cloud-firestore axios