阿里云函数nodejs-mongodb,存储浮点数兼容函数
在云函数中将浮点数存储到mongodb时,数据库会自动转为对象的bug (num字段应该为浮点数类型):
"_id": "5f44cdd367f1376654bef7a8" "num": "high": 3471712362749231000 "low": 15648 "negative": false "naN": false "infinite": false "finite": true
安装
npm i -s aliyun-mdb-floatfix
使用
方式一: 在获取数据后,进行一次转换
const deepParseFloat = ; let result = await clouddb result = //深度遍历转换对应的对象为浮点数
方式二: 统一挂入钩子
云函数端
const hook = ; exportsget = async { const cloud = contextcloud; const openId = contextopenId; ; //挂入钩子 当调用find aggregate函数结果会自动处理浮点数对象 let result = await clouddb return result0openId;};
小程序端
;; cloud; ; //挂入钩子 当调用find aggregate函数结果会自动处理浮点数对象 let getData = async { let result = await clouddb console; return result}