Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How get a Decimal128 as string directly? #6268

Closed
islishude opened this issue Mar 23, 2018 · 5 comments
Closed

How get a Decimal128 as string directly? #6268

islishude opened this issue Mar 23, 2018 · 5 comments
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.

Comments

@islishude
Copy link

my document has a Decimal128 type field When I use by modle.findOne({}) in MongoShell, It would print this field as stirng type

but in mongoose I got a Object, What should I do to get a Decinal128 as string output instead of use toString method ?

I found no idea in doc and Google .

@gianpaj
Copy link
Contributor

gianpaj commented May 27, 2018

how did you fix this?

i upgraded from v5.0.12 to v5.1.2 and now toJSON is not returning the Decimal128 or Decimal.
Which one is the correct schema type definition btw?

model

    price: {
      type: Schema.Types.Decimal128,
      required: true,
    },
ProductSchema.set('toJSON', {
  getters: true,
  transform: (doc, ret) => {
    ret.price = ret.price.toString();
    delete ret.id;
    delete ret.__v;
    return ret;
  },
});

ret.price is null when I convert it to JSON

controller

// create product

const product = new Product({
    price: req.body.price,
  });

product.save();

// get products (actually using mongoose static method)
const products = async Product.find();
res.json({ data: products }))

@vkarpov15 vkarpov15 reopened this May 29, 2018
@vkarpov15 vkarpov15 added this to the 5.1.4 milestone May 29, 2018
@vkarpov15 vkarpov15 added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label May 29, 2018
@vkarpov15
Copy link
Collaborator

Thanks for reporting, will investigate asap 👍

@gianpaj
Copy link
Contributor

gianpaj commented May 30, 2018

Actually, mongoose seems fine:

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/mongoose-issue-6228');

mongoose.connection.on(
  'error',
  console.error.bind(console, 'connection error:')
);

const ProductSchema = new mongoose.Schema({
  price: {
    type: mongoose.Schema.Types.Decimal128,
    required: true,
  },
});

ProductSchema.set('toJSON', {
  transform: (doc, ret) => {
    ret.price = ret.price.toString();
    return ret;
  },
});

const Product = mongoose.model('Product', ProductSchema);

const product = new Product({
  price: '123',
});

product.save();

Product.findOne().then(p => {
  console.log(p.toJSON());
  // { _id: 5b0e8059f17b64c477e5b171, price: '123', __v: 0 }
});

tested on mongoose@5.1.3

might be something else with my Node.JS server (express, etc.)

@vkarpov15 vkarpov15 added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Jun 1, 2018
@vkarpov15 vkarpov15 removed this from the 5.1.4 milestone Jun 1, 2018
@vkarpov15
Copy link
Collaborator

@gianpaj thanks for looking into it. Let me know if you manage to figure out what the cause of this issue is for you

@islishude
Copy link
Author

@gianpaj please open a new issue if you still have this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.
Projects
None yet
Development

No branches or pull requests

3 participants