【问题标题】:How do I get the decimal in the right place with mongoose-currency?如何使用猫鼬货币在正确的位置获得小数点?
【发布时间】:2021-02-17 11:15:20
【问题描述】:

我在我的用户模型中实现了mongoose-currency

const mongoose = require("mongoose");
const { Schema } = mongoose;
require("mongoose-currency").loadType(mongoose);
const Currency = mongoose.Types.Currency;

const userSchema = new Schema({
  googleId: String,
  paid: { type: Currency },
});

mongoose.model("users", userSchema);

但即使我在客户端添加toFixed(2)

import React, { Component } from "react";
import { connect } from "react-redux";
import { Link } from "react-router-dom";

class Header extends Component {
  renderContent() {
    switch (this.props.auth) {
      case null:
        return;
      case false:
        return (
          <li>
            <a href='/auth/google'>Login With Google</a>
          </li>
        );

      default:
        return [
          <li key='1'>Paid: ${this.props.auth.paid.toFixed(2)}</li>,
          <li key='2'>
            <a href='/api/logout'>Logout</a>
          </li>,
        ];
    }
  }

我得到$1000.00 而不是$10.00,如何在不添加两个零的情况下让小数点起作用?

所以我尝试了这种方法:

const mongoose = require("mongoose");
const { Schema } = mongoose;

const userSchema = new Schema({
  googleId: String,
  paid: { type: Number, get: getPaid },
});

const getPaid = userSchema.path("paid").get(function (p) {
  return (p / 100).toFixed(2);
});

mongoose.model("users", userSchema);

但我得到了ReferenceError: Cannot access 'getPaid' before initialization

【问题讨论】:

    标签: reactjs mongoose


    【解决方案1】:

    您应该像这样为您的架构定义一个吸气剂:

    userSchema.path('paid').get(function(p) {
      return (p / 100).toFixed(2);
    });
    

    添加: 无需像这样引用您的吸气剂:

    paid: { type: Number, get: getPaid },
    

    当您尝试访问路径时,Getter 会起作用。所以你应该这样做:

    const userSchema = new Schema({
      googleId: String,
      paid: { type: Number },
    });
    
    userSchema.path("paid").get(function (p) {
      return (p / 100).toFixed(2);
    });
    

    但是,这里有一个问题:为了触发 getter,您需要显式访问路径。因此,当您搜索您的收藏时,您需要从返回的文档中调用路径:

    userSchema.findById('5fc9c809fc360d373b3d77b8')
        .then(u => {
            console.log(u) // <- this will show the original "paid" path
            console.log(u.paid) // <- This will give you the formatted "paid" path
        })
    

    仅供参考,要按照您尝试的方式进行操作,您需要像这样编写代码(这将完成与上述相同的事情):

    const userSchema = new Schema({
      googleId: String,
      paid: { type: Number, get: getPaid },
    });
    
    function getPaid(p) {
      return (p / 100).toFixed(2);
    });
    

    【讨论】:

    • 正如您发布答案一样,当我在初始化之前无法访问上述内容时,我正在努力解决如何访问上述内容,我将在上面添加它。
    【解决方案2】:

    虽然提供的解决方案在逻辑上应该可行,但它对我不起作用,因为我会遇到有关初始化的参考错误问题,当我解决这个问题时,它仍然会以$ 1000.00 支付 10 美元,所以我终于来了想出一个适合我的解决方案:

    import React, { Component } from "react";
    import { connect } from "react-redux";
    import { Link } from "react-router-dom";
    
    class Header extends Component {
      parsedNumber() {
        return (this.props.auth.paid / 100).toFixed(2);
      }
      renderContent() {
        switch (this.props.auth) {
          case null:
            return;
          case false:
            return (
              <li>
                <a href='/auth/google'>Login With Google</a>
              </li>
            );
    
          default:
            return [
              <li key='1'>Paid: ${this.parsedNumber()}</li>,
              <li key='2'>
                <a href='/api/logout'>Logout</a>
              </li>,
            ];
        }
      }
    

    经过如此多的试验和错误以及 codemonkey 等同事的帮助后,我意识到我别无选择,只能设法让这些小数位显示在 paid 属性正在呈现的点上,所以我想出了上面的方法,它可以工作。现在我得到$10.00 而不是$1000.00

    【讨论】:

      猜你喜欢
      • 2011-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-09
      • 2016-04-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多