微信小程序数据库的使用

WX数据库

https://developers.weixin.qq.com/miniprogram/dev/wxcloud/basis/capabilities.html#数据库

集合JSON

[
  {
    _id: 'Wzh76lk5_O_dt0vO',
    title: 'The Catcher in the Rye',
    author: 'J. D. Salinger',
    characters: [
      'Holden Caulfield',
      'Stradlater',
      'Mr. Antolini'
    ],
    publishInfo: {
      year: 1951,
      country: 'United States'
    }
  },
]

数据库使用

// 1. 获取数据库引用
const db = wx.cloud.database()
// 2. 构造查询语句
// collection 方法获取一个集合的引用
// where 方法传入一个对象,数据库返回集合中字段等于指定值的 JSON 文档。API 也支持高级的查询条件(比如大于、小于、in 等),具体见文档查看支持列表
// get 方法会触发网络请求,往数据库取数据
db.collection('books').where({
  publishInfo: {
    country: 'United States'
  }
}).get({
  success(res) {
  // 输出 [{ "title": "The Catcher in the Rye", ... }]
    console.log(res)
  }
})


// return new Promise((resolve, reject) => {
//     getPageData.getData('https://used-api.jd.com/auction/list', {
//             pageNo: pageNo,
//             pageSize: 100,
//             category1: category1
//         }).then((res) => {
//             console.log('pageNo:', pageNo)
//             console.log(res.data.auctionInfos)
//             _this.saveData(res.data.auctionInfos)
//             resolve('success')
//         })
//         .catch((err) => {
//             console.error(err)
//             reject(err)
//         })
// })


wx.cloud.callFunction({
    // 云函数名称
    name: 'getPageData',
    // 传给云函数的参数
    data: {
        pageNo: pageNo,
        pageSize: 100,
        category1: category1,
    },
    success(res) {
        console.log('in');
        // console.log(pageNo);
        console.log(res.result);
        console.log('in');
    },
    fail: console.error
})

return new Promise((resolve, reject) => {
            wx.request({
                url: 'https://used-api.jd.com/auction/list',
                data: {
                    pageNo: pageNo,
                    pageSize: 100,
                    category1: category1
                },
                header: {
                    'content-type': 'application/json' // 默认值
                },
                success(res) {
                    console.log('nin');
                    return {
                        auctionInfos: res.data.data.auctionInfos
                    }
                }
            })
        })