Problem using findOne in Node JS to find a document with a Number in it

Viewed 129

I'm building a simple app and in this case using express and EJS, I'm just accessing some data in my database. My database is MongoDB and I'm accessing it with mongoose. All goes well when I use a find and findOne like those:

router.post('/ConsProducto', async (req, res) => {
    const productos= await Productos.find();
    console.log(productos);

That gives

[
  {
    _id: new ObjectId("61982502b4d3d91817558358"),
    codigo_producto: 1,
    nombre_producto: 'Melocotones',
    nitproveedor: 1,
    precio_compra: '25505',
    ivacompra: 215,
    precio_venta: 30351
  },
  {
    _id: new ObjectId("61982502b4d3d91817558359"),
    codigo_producto: 2,
    nombre_producto: 'Manzanas',
    nitproveedor: 3,
    precio_compra: '18108',
    ivacompra: 19,
    precio_venta: 21549
  }

All the way until 19, those are the elements that I posted on that database. With findOne

router.post('/ConsProducto', async (req, res) => {
    const products = await Productos.findOne({ nombre_producto: "Queso"});
    console.log(products);

That gives

{
  _id: new ObjectId("61982502b4d3d91817558362"),
  codigo_producto: 11,
  nombre_producto: 'Queso',
  nitproveedor: 5,
  precio_compra: '26571',
  ivacompra: 19,
  precio_venta: 31619
}

All great, but when I try to use a number, I get null. I have tried using casts to convert it, like Number() or NumberInt(), using parseInt(), because in the database appears to be an Int32, but nothing works. What am I doing wrong?

router.post('/ConsProducto', async (req, res) => {
    const products = await Productos.findOne({ codigo_producto: NumberInt(2)});
    console.log(products);

That gives me

null

Also when created without cast

router.post('/ConsProducto', async (req, res) => {
    const products = await Productos.findOne({ codigo_producto: '2'});
    console.log(products);

That gives me

null

Adding the module of "Productos" just in case somebody needs it

const mongoose = require('mongoose');
const Schema =mongoose.Schema;

const ProductosSchema = new Schema({
    codigo_producto: Number,
    nombre_producto: String,
    nitproveedor:Number,
    precio_compra:String,
    ivacompra:Number,
    precio_venta: Number    
});

module.exports = mongoose.model('db_productos',ProductosSchema);

Adding the way of insertion

 csvtojson().fromFile("./public/upload/productos.csv").then(source => {
                    var results = [];
                    for (var i = 0; i < source.length; i++) {
                        var linea = {
                            codigo_producto: source[i]["codigo_producto"],
                            nombre_producto: source[i]["nombre_producto"],
                            nitproveedor: source[i]["nitproveedor"],
                            precio_compra: source[i]["precio_compra"],
                            ivacompra: source[i]["ivacompra"],
                            precio_venta: source[i]["precio_venta"]
                        
                        };
                        results.push(linea);
                    }
                    console.log(results);
                    var collectionName = "db_productos";
                    var collection = mongoose.connection.db.collection(collectionName);
                    collection.insertMany(results, {ordered:false} ,(err, result) => {
                        if (err) console.log(err);
                        if (result) {
                            console.log("Successfull");
                        }
                    });
0 Answers
Related