weixin_46394824 2020-04-09 12:13 采纳率: 0%
浏览 242

无法获取数据库中搜索框返回的值

新手做了一个小程序,想实现搜索某个词,如果数据库name字段中包含,这返回显示在搜索结果也中,代码如下:
result,wxml

<view class="weui-search-bar">
  <view class="weui-search-bar__form">
    <view class="weui-search-bar__box">
      <icon class="weui-icon-search_in-box" type="search" size="14"></icon>
      <input type="text" class="weui-search-bar__input"  disabled='true' placeholder="搜索入口" bindtap="wxSearchTab"/>
    </view>
  </view>
</view>
<view>我们为你搜索到关于{{searchValue}}的内容,结果如下:</view>
<view class="service-list">
    <navigator class="service-item" wx:for="{{data}}" wx:key="{{item.id}}" url="../proInfo/index?id={{item.id}}">
                <view class="service-img">
                    <image src="{{item.pic}}" mode="aspectFill"></image>
                </view>
                <view class="service-text">
                    <view class="service-tite">{{item.name}}</view>
                    <text class="service-city">价格:{{item.price}}</text>
                </view>
    </navigator>
</view>

result.js

//index.js


Page({
  data: {
    searchValue:{},
    data:[],   
  },

// 搜索页面跳回
 onLoad: function (options) {
   if (options && options.searchValue) {
     this.setData({
       searchValue: options.searchValue
     });

      const db = wx.cloud.database()
      const _ = db.command
      db.collection('searchlist').where({
        name: _.in(['searchValue'])
      }).get({
        success: res => {
        this.setData({
          data: res.data
        })
        console.log('[数据库] [查询记录] 成功: ', res)
        },
        fail: err => {
          wx.showToast({
            icon: 'none',
            title: '查询记录失败'
          })
          console.error('[数据库] [查询记录] 失败:', err)
        }
   })
   }
},

  // 搜索入口  
  wxSearchTab: function () {
    wx.redirectTo({
      url: '../search/search'
    })
  }
})

页面显示结果如图:
图片说明
数据库:
图片说明

console返回结果:
[数据库] [查询记录] 成功: {data: Array(0), errMsg: "collection.get:ok"}

请教是什么情况呢?为什么数据库查询成功而没有返回数据呢?

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-25 16:02
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    根据您提供的代码和返回的结果,我观察到了一些问题。首先,this.dataonLoad 函数中被初始化为一个空数组,这可能是由于您在 init 方法中未调用 setData 来更新这个数据。其次,您的 wxSearchTab 函数在 onLoad 中被调用了两次,导致了重复的数据加载。

    解决这些问题的方法是将 wxSearchTab 函数放在正确的生命周期方法(例如 onLoadonReady) 中,并且在其中正确地使用 this.setData 更新数据。

    此外,wx.redirectTo 是用于跳转到其他页面的函数,而不是用于重定向到搜索结果页面。如果您想要从搜索结果页面跳转到其他页面,请确保您已经正确设置了目标页面的 URL。

    以下是修改后的代码示例:

    Page({
      data: {
        searchValue: '',
        data: [],
      },
    
      onLoad: function(options) {
        if (options && options.searchValue) {
          this.setData({ searchValue: options.searchValue });
        }
    
        const db = wx.cloud.database()
        const _ = db.command
    
        // 这里应该移除掉重定向的逻辑,因为这不会触发搜索结果页面的跳转
        // const searchList = db.collection('searchlist').where({
        //   name: _.contains(['searchValue']),
        // }).get({
        //   success: res => {
        //     this.setData({ data: res.data })
        //   },
        //   fail: err => {
        //     console.error('[数据库] [查询记录] 失败:', err)
        //   }
        // })
    
        // 现在,您可以直接在这里处理数据并执行其他操作
        this.handleData();
      },
    
      handleData() {
        const db = wx.cloud.database()
        const _ = db.command
    
        db.collection('searchlist').where(_.contains(['searchValue'])).get({
          success: res => {
            this.setData({ data: res.data })
            console.log('[数据库] [查询记录] 成功: ', res)
          },
          fail: err => {
            console.error('[数据库] [查询记录] 失败:', err)
          }
        })
      },
    
      // 搜索入口  
      wxSearchTab: function () {
        wx.redirectTo({
          url: '../search/search'
        })
      }
    });
    

    这样,当用户点击搜索按钮时,它会重定向到搜索结果页面,同时也会加载新的数据。

    评论

报告相同问题?

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)