search
HomeWeChat AppletMini Program DevelopmentHow to use cloud functions to query data in mini programs

How to use cloud functions to query data in mini programs

Mar 24, 2020 am 11:51 AM
Cloud functionAppletsQuery data

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(),实现在云函数中查询数据
}

Demonstration of querying data in cloud functions

Recommended learning:Small program development

①Interface

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

②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)
  }
})

③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()
}

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!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version