当前位置:K88软件开发文章中心编程全书微信小程序 → 文章内容

微信小程序云开发服务端数据库API 构造一个服务端时间的引用

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-15 15:03:26

由 Carrie 创建, 最后一次修改 2018-11-15 db.serverDate构造一个服务端时间的引用。可用于查询条件、更新字段值或新增记录时的字段值。方法签名如下:function serverDate(options?: object): ServerDate方法接受一个可选对象参数 options,其字段定义如下:字段名类型必填默认值说明offsetnumber否引用的服务端时间偏移量,毫秒为单位,可以是正数或负数示例代码新增记录时设置字段为服务端时间:const cloud = require('wx-server-sdk')cloud.init()const db = cloud.database()exports.main = async (event, context) => { try { return await db.collection('todos').add({ description: 'eat an apple', createTime: db.serverDate() }) } catch(e) { console.error(e) }}更新字段为服务端时间往后一小时:const cloud = require('wx-server-sdk')cloud.init()const db = cloud.database()exports.main = async (event, context) => { try { return await db.collection('todos').doc('my-todo-id').update({ due: db.serverDate({ offset: 60 * 60 * 1000 }) }) } catch(e) { console.error(e) }}``

微信小程序云开发服务端数据库API 构造一个服务端时间的引用