小程序云开发后端js

// 云函数入口文件

const cloud = require('wx-server-sdk')

const TcbRouter = require('tcb-router')

cloud.init()

const db = cloud.database()

// 云函数入口函数

exports.main = async (event, context) => {

  const app = new TcbRouter({

    event

  })

// 添加记录的路由

app.router('add', async (ctx, next) => {

  //可以直接调用event

  ctx.body = await db.collection('goods').add({

      data: {

        info: event.info,

        address: event.address,

        desc: event.desc

      }

    })

    .then((res) => {

      return res

    })

})

// 获取指定商品信息

app.router('get', async (ctx, next) => {

  let _id=event._id

  ctx.body = await db.collection('goods').where({

    _id

  }).get()

    .then((res) => {

      return res

    })

})

// 获取所有商品信息

app.router('getall', async (ctx, next) => {

  ctx.body = await db.collection('goods').get()

    .then((res) => {

      return res

    })

})

// 更新商品信息

app.router('updata', async (ctx, next) => {

  ctx.body = await db.collection('goods').where({

    _id:event._id

  }).update({

    data:{

      info:event.info

    }

  })

    .then((res) => {

      return res

    })

})

// 删除商品信息

app.router('del', async (ctx, next) => {

  ctx.body = await db.collection('goods').where({

    _id: event._id

  }).remove()

    .then((res) => {

      return res

    })

})

return app.serve()

}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容