# aliyun-mdb-floatfix

> 在云函数中将浮点数存储到mongodb时，数据库会自动转为对象的bug (num字段应该为浮点数类型)： ```javascript { "_id": "5f44cdd367f1376654bef7a8", "num": { "high": 3471712362749231000, "low": 15648, "negative": false, "naN": false, "infinite": false, "finite":

Latest version **1.0.3** (published 2020-09-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install aliyun-mdb-floatfix
pnpm add aliyun-mdb-floatfix
yarn add aliyun-mdb-floatfix
bun add aliyun-mdb-floatfix
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2020-09-22 |
| First published | 2020-08-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 4.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | 137757831@qq.com |
| Maintainers | tangoboy |
| Keywords | float, aliyun, cloud, mongodb |

## Links

- npm: https://www.npmjs.com/package/aliyun-mdb-floatfix
- npm.io page: https://npm.io/package/aliyun-mdb-floatfix

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 1.0.3 (latest) — 2020-09-22
- 1.0.2 — 2020-09-14
- 1.0.1 — 2020-09-14
- 1.0.0 — 2020-08-25

## README

# 阿里云函数nodejs-mongodb，存储浮点数兼容函数

在云函数中将浮点数存储到mongodb时，数据库会自动转为对象的bug (num字段应该为浮点数类型)：
```javascript
{
  "_id": "5f44cdd367f1376654bef7a8",
  "num": {
    "high": 3471712362749231000,
    "low": 15648,
    "negative": false,
    "naN": false,
    "infinite": false,
    "finite": true
  }
}
```

## 安装

`npm i -s aliyun-mdb-floatfix`


## 使用

方式一: 在获取数据后，进行一次转换

```javascript
const deepParseFloat = require('aliyun-mdb-floatfix');


let result = await cloud.db.collection('float').find(
        {  
            _id: '5f44cdd367f1376654bef7a8' 
        }, 
        {  
            limit: 1, 
        }
    )
    
    
    result = deepParseFloat(result) //深度遍历转换对应的对象为浮点数

```


方式二: 统一挂入钩子


云函数端

```javascript

const { hook } = require('aliyun-mdb-floatfix');

exports.get = async (context) => {
    const cloud = context.cloud; 
    const openId = context.openId;

    hook(cloud);  //挂入钩子 当调用find aggregate函数结果会自动处理浮点数对象

    let result = await cloud.db.collection('float').find(
        {  
            _id: '5f44cdd367f1376654bef7a8' 
        }, 
        {  
            limit: 1, 
        }
    )
    return result[0][openId];
};

```

小程序端

```javascript
import cloud from '@tbmp/mp-cloud-sdk';
import { hook } from 'aliyun-mdb-floatfix';

cloud.init({
  env: 'test'
});

hook(cloud); //挂入钩子 当调用find aggregate函数结果会自动处理浮点数对象

let getData = async ()=>{
  let result = await cloud.db.collection('float').find(
          {  
              _id: '5f44cdd367f1376654bef7a8' 
          }, 
          {  
              limit: 1, 
          }
      )
  console.log(result);
  return result
}

```

---
_Source: https://npm.io/package/aliyun-mdb-floatfix · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
