-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Comments
how did you fix this? i upgraded from v5.0.12 to v5.1.2 and now toJSON is not returning the
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;
},
});
// 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 })) |
Thanks for reporting, will investigate asap 👍 |
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 might be something else with my Node.JS server (express, etc.) |
@gianpaj thanks for looking into it. Let me know if you manage to figure out what the cause of this issue is for you |
@gianpaj please open a new issue if you still have this problem. |
my document has a Decimal128 type field When I use by
modle.findOne({})
in MongoShell, It would print this field as stirng typebut 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 .
The text was updated successfully, but these errors were encountered: