【发布时间】:2022-01-25 07:02:30
【问题描述】:
如何授予对要在函数内部使用的 const 的访问权限?在这种情况下,我想在我的函数 fetchListings 中访问我的 const catName。我收到此错误:
问题更新:
ReferenceError: catName is not defined
<script context="module">
const fetchListings = async () => {
try {
// get reference to listings collection
const listingsRef = collection(db, 'listings');
// create a query to get all listings
const q = query(
listingsRef,
where('type', '==', catName),
orderBy(timestamp, 'desc'),
limit(10)
);
// execute query
const querySnap = await getDocs(q);
let lists = [];
querySnap.forEach((doc) => {
console.log(doc);
});
} catch (error) {
console.log(error);
}
};
fetchListings();
</script>
<script>
import { page } from '$app/stores';
// export the const catName to the function above
export const catName = $page.params.catName;
</script>
Hi {catName}!
【问题讨论】:
-
您可能打算使用
where('type', '==', catName),而不是where(type, '==', catName), -
谢谢,现在我得到了正确的错误:
ReferenceError: catName is not defined -
fetchListings的模块是否导入了定义catName的模块? -
fetchListings(catName)?我在考虑范围问题?
标签: javascript svelte sveltekit