小程序开发中的云函数-添加数据篇

news/2024/7/20 3:48:37 标签: 小程序

1.云函数中的数据添加需要在云函数中做以下三步:
(1)sdk的引用与初始化
(2)数据库引用对象的创建
(3)数据库的链接
具体代码如下:

// 云函数入口文件,此句为对sdk的引入
const cloud = require('wx-server-sdk')

//初始化sdk的cloud
cloud.init()

//创建数据库的引用对象db
const db = cloud.database()

// 云函数入口函数
exports.main = async (event, context) => {
  //对数据库expressDate进行连接,并使用add()方法向数据库中添加数据
  return await db.collection('expressDate').add({
    data:{
      code:"1678",
      company:"京东",
      describe:"衣物",
      phone:"3567"
    }
  })
}

2.在wxml文件中创建点击事件

<!-- 使用bindtap属性绑定点击事件,其中bindtap后的值为你将要在js文件中创建的函数名 -->
<button bindtap="addFuction">添加数据</button>

3.在页面的js文件中对点击事件进行处理

// res参数是用来接收云函数中的返回值的,没有它你无法接收到云函数执行后的结果
  addFuction(res){
    // 调用云函数的固定语句
    wx.cloud.callFunction({
      // 使用name标明所要链接的云函数
      name:"addDate",
      // 使用data向云函数中传递数据
      data:{a:1}
    }).then(console.log(res))//使用then打印云函数返回的结果
  }

http://www.niftyadmin.cn/n/1028057.html

相关文章

C++核心准则ES.106:不要试图通过使用无符号类型避免负值

ES.106: Dont try to avoid negative values by using unsigned ES.106:不要试图通过使用无符号类型避免负值 Reason&#xff08;原因&#xff09; Choosing unsigned implies many changes to the usual behavior of integers, including modulo arithmetic, can suppress w…

C++核心准则ES.107:不要使用无符号数下标,使用gsl::index更好

ES.107: Dont use unsigned for subscripts, prefer gsl::index ES.107:不要使用无符号数下标&#xff0c;使用gsl::index更好 Reason&#xff08;原因&#xff09; To avoid signed/unsigned confusion. To enable better optimization. To enable better error detection. …

小程序开发中的云函数-删除数据篇

1.在云函数的js文件中需要做三件事: (1)sdk的引用及初始化 (2)数据库引用对象的创建 (3)利用引用对象对数据库数据库进行链接 具体代码如下: // 云函数入口文件,此句为对sdk的引入 const cloud require(wx-server-sdk)//初始化sdk的cloud cloud.init()//创建数据库的引用对象…

单例的多线程使用

前一篇文章介绍了什么是单例模式以及如何使用单例模式。那么在多线程程序中我们如何使用单例模式呢&#xff1f;也就是说在多线程中我们如何保证类实例的唯一性&#xff1f; 众所周知&#xff0c;多线程中使用较多的是锁lock。 lock是确保一个线程位于代码的临界区时&#xff0…

C++核心准则Per.1,2,3,4 慎重地优化代码

Per.1: Dont optimize without reason Per.1:没有明确理由不要进行优化 Reason&#xff08;原因&#xff09; If there is no need for optimization, the main result of the effort will be more errors and higher maintenance costs. 如果没有优化的需求&#xff0c;这种…

js学习-第3天(2)

对象的基本知识 JavaScript 是使用“对象化编程”的&#xff0c;或者叫“面向对象编程”的。所谓“对象化编程”&#xff0c;意思是把 JavaScript 能涉及的范围划分成大大小小的对象&#xff0c;对象下面还继续划分对象直至非常详细为止&#xff0c;所有的编程都以对象为出发点…

【转】实例讲解override和new的区别

一、override&#xff1a;Override关键字主要是提供派生类对基类方法的新实现。 1、不可以用于重写非虚方法和静态方法 2、与其配套使用的关键字是Virtual、abstract、Override。 3、不能使用修饰符 new、static、virtual 或 abstract 来修改 override 方法。 二、new&#xff…

C++核心准则Per.5,6 关于性能的误解

Per.5: Dont assume that low-level code is necessarily faster than high-level code Per.5:不要主观的认为低层次代码一定比高层次代码快 Reason&#xff08;原因&#xff09; Low-level code sometimes inhibits optimizations. Optimizers sometimes do marvels with hi…