Giter VIP home page Giter VIP logo

tcloudbase's Introduction

tcloudbase

作业4 将教程范例(意见反馈平台DEMO)中的管理端部分的列表加载变成实时数据库监听形式触发。

/**

  • 加载意见列表(调用云函数:init) */ function initlist() { const db = cloud.database() const _ = db.command db.collection('advice') // 获取反馈不为空的数据 .where({ advice: _.neq("") }) // 实时推送 // 获取结果为 0 时需要清除浏览器缓存和 cookie .watch({ onChange: res => { console.log(res.docs); // 处理日期对象显示异常 let list = res.docs.map(item => { item.adddue = new Date(item.adddue.$date); return item; }) refreshlist(list) }, onError: err => { console.error(err); } }) }

用户端列表加载云函数适配超过100条的场景,采用promise all的形式进行改造,使其可以支持超过100条

const pagesize = 100; exports.main = async (event, context) => { let res = {}; const auth = cloud.auth().getUserInfo(); const uid = auth.uid; if(uid!=null){ const ids = (await db.collection('admin').where({ _id:uid }).get()).data; if(ids.length!=0){ console.log(ids[0]); const countResult = await db.collection('advice').count(); // 获得数据库总条数 const total = countResult.total; // 计算分页次数 const batchTimes = Math.ceil(total / pagesize); const tasks = [] // 循环添加 Promise 请求 for (let i = 0; i < batchTimes; i++) { const promise = await db.collection('advice').skip(i * pagesize).limit(pagesize).orderBy('adddue', 'desc').get(); tasks.push(promise); } res.list = (await Promise.all(tasks)).reduce((acc, cur) => { // 使用 reduce 拼接请求结果 return { data: acc.data.concat(cur.data), errMsg: acc.errMsg, } }).data; res.code = 0; } else{ res.code = 1; }

tcloudbase's People

Contributors

bluewatercg avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.