How to use cloud functions to query data in mini programs

angryTom
Release: 2020-03-24 11:52:07
forward
4714 people have browsed it

This article introduces the method of using cloud functions in cloud development to query data in WeChat mini programs. It has certain reference value. I hope it will be helpful to friends who are learning mini programs!

How to use cloud functions to query data in mini programs

How to use cloud functions to query data in small programs

Querying data in cloud functions requires initializing the Cloud SDK

// 云函数入口文件
const cloud = require('wx-server-sdk')//引入Cloud SDK
cloud.init()//对Cloud SDK初始化
const db = cloud.database()//初始化完成后,引出database
// 云函数入口函数
exports.main = async (event, context) => {
    const await db.collection('todos').get()
  }//在main函数返回collection().get(),实现在云函数中查询数据
}
Copy after login

Demonstration of querying data in cloud functions

Recommended learning:Small program development

①Interface

<!-- index.wxml -->
<button bindtap="cloudFunction">调用云函数</button>
Copy after login

②Click the button, Trigger the cloudFunction event and call the cloud function queryData

//index.js-index
Page({
  cloudFunction:function(){
    console.log("Button is click")//测试按钮是否被按下
    wx.cloud.callFunction({//调用云函数
      name: "queryData"// 要调用的云函数名称
    }).then(console.log)
  }
})
Copy after login

③Create the cloud function queryData

cloudfunctions=>Right-click and select: New node.js cloud function

③Create in the cloud function A data collection location

cloudfunctions=>Right-click and select: New node.js cloud function

④Write some code and upload it to the cloud

//index.js-queryData
// 云函数入口文件
const cloud = require(&#39;wx-server-sdk&#39;)
cloud.init()
const db = cloud.database()
// 云函数入口函数
exports.main = async (event, context) => {
 return await db.collection("location").get()
}
Copy after login

Final running result

How to use cloud functions to query data in mini programs

The above is the detailed content of How to use cloud functions to query data in mini programs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template