亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

檢查 nuxt 中的空查詢參數

檢查 nuxt 中的空查詢參數

慕虎7371278 2023-06-29 21:18:47
我的 Prismic/Nuxt 項目上有一個搜索頁面,可以對 Prismic API 進行全文查詢。這段代碼將做到這一點export default {  name: 'Search',  async asyncData({ $prismic, params, query, error }) {    try {      // Query to get post content      const products = await $prismic.api.query($prismic.predicates.fulltext('my.product.title', query.q), { orderings: '[my.product.title desc]' })      // Returns data to be used in template          return {        products: products.results,      }    } catch (e) {          // Returns error page      error({ statusCode: 404, message: 'Page not found' })    }  },}網址是/search/?q=somesearch問題是,如果我在沒有查詢參數的情況下點擊 /search/ ,它會立即捕獲錯誤Unable to encode undefined of type undefined,顯然是因為它嘗試使用未定義的值查詢 API,但我似乎不知道如何制作如果未設置查詢,則不查詢 API,而是僅顯示搜索框,這是有效的檢查。我嘗試過if query.q !== undefined 在嘗試部分,但這不起作用。
查看完整描述

1 回答

?
繁星coding

TA貢獻1797條經驗 獲得超4個贊

要檢查未定義的值,您可以使用:

if (typeof query.q !== 'undefined')

typeof以字符串形式返回數據類型,這使得檢查未定義的值成為可能。


查看完整回答
反對 回復 2023-06-29
  • 1 回答
  • 0 關注
  • 142 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號