Javascript 如何在查询firestore数据库之前检查参数有效性?
我正在尝试使用Firebase云函数返回用户是否具有内容权限。我想我会很清楚我希望它如何基于代码运行 本质上,我希望在执行复合查询之前检查以确保用户存在并且内容存在。这是因为当我执行双重查询时,它将返回“此用户无权访问此内容”,即使真正的问题是请求的contentId或userId不正确Javascript 如何在查询firestore数据库之前检查参数有效性?,javascript,node.js,firebase,google-cloud-firestore,Javascript,Node.js,Firebase,Google Cloud Firestore,我正在尝试使用Firebase云函数返回用户是否具有内容权限。我想我会很清楚我希望它如何基于代码运行 本质上,我希望在执行复合查询之前检查以确保用户存在并且内容存在。这是因为当我执行双重查询时,它将返回“此用户无权访问此内容”,即使真正的问题是请求的contentId或userId不正确 function checkUser(userId) { db.collection("users") .where("userId", "==", userId) .get() .
function checkUser(userId) {
db.collection("users")
.where("userId", "==", userId)
.get()
.then(snapshot => {
if (snapshot.empty) {
return true;
} else {
return false;
}
})
.catch(err => console.error(err));
}
function checkContent(contentId) {
db.collection("content")
.doc(contentId)
.get()
.then(snapshot => {
if (!snapshot.exists) {
return true;
} else {
return false;
}
})
.catch(err => console.error(err));
}
app.get("/access", (req, res) => {
db.collection("access")
.where("userId", "==", req.body.userId)
.where("contentId", "==", req.body.contentId)
.get()
.then(snapshot => {
if (checkContent(req.body.contentId)) {
return res.status(404);
}
if (checkUser(req.body.userId)) {
return res.status(404);
}
if (snapshot.empty) {
return res.status(401).json({
message: "The requested user does not have access to this content"
});
} else {
return res.status(200).json({
message: "The requested user has access to this content"
});
}
})
.catch(err => {
console.error(err);
return res
.status(500)
.json({ message: "something went wrong on our end" });
});
});
我假设这与正在进行的异步代码有关,但我不确定如何修复它
谢谢你的帮助
为@Chris G编辑
const checkUser = async function(userId){
return db.collection("users")
.where("userId", "==", userId)
.get()
.then(snapshot => {
if (snapshot.empty) {
return true;
} else {
return false;
}
})
.catch(err => console.error(err));
}
const checkContent = async function(contentId){
return db.collection("access")
.where('contentId', '==', contentId)
.get()
.then(snapshot => {
if (snapshot.empty) {
return true;
} else {
return false;
}
})
.catch(err => console.error(err));
}
app.get("/access", (req, res) => {
db.collection("access")
.where("userId", "==", req.body.userId)
.where("contentId", "==", req.body.contentId)
.get()
.then(snapshot => {
const contentCheck = await checkContent(req.body.contentId);
const userCheck = await checkUser(req.body.userId);
.....
请看,这是哪一个。这里没有处理“干预”调用的异步性质的内容,除了您希望首先检查用户是否存在,然后检查他们是否有访问权限之外。另外,请注意
checkUser
实际返回的内容:undefined
。内部函数返回true
/false
,这与checkUser
的返回值无关。返回true在checkUser函数的深处,code>不会从该函数返回,而是从传递给的回调函数返回。then()
。您需要研究使用await
@ChrisG是否有任何方法可以提供有关如何重新构造函数以返回true/false的详细信息如果在db.collection(…
)之前添加return
,函数将返回承诺。这意味着您可以执行var result=await checkUser(…)
其他地方。每个内部有wait
的函数都需要声明为async
,就像错误所建议的那样。基本示例:请看,这是重复的。这里没有任何内容涉及“干预”的异步性质调用,除此之外,您需要首先检查用户是否存在,然后检查他们是否有访问权限。另外,请注意checkUser
实际返回的内容:undefined
。内部函数返回true
/false
,这与checkUser
的返回值无关。return true;
在您的checkUser
函数的深处,它不会从该函数返回,而是从您传递给的回调函数返回
。您需要研究使用await
@ChrisG是否有任何方法可以向我提供有关如何重新构造函数以返回true/false的详细信息如果在db.collection(…
)之前添加return
,函数将返回承诺。这意味着您可以执行var result=await checkUser(…)
其他位置。每个内部有wait
的函数都需要声明为async
,与错误提示完全相同。基本示例: