Mongoose throw error while saving a lot of rows

228 views
Skip to first unread message

reda khyatti

unread,
Feb 18, 2014, 9:40:46 AM2/18/14
to nod...@googlegroups.com
Hi all,

I am a newbie in Node, and I am trying to rewrite a program that syncs databases in a non blocking env (Node). So the first task is to make a request to a SQL server, which I did successfully using 'mssql'. This request retruns more than 30k rows that I would like to persist to a MongoLab Document.
I have therefore implemented the model and the method to save the row. The operation at first look good, since I am able to write things to the db, however I get this error after adding few hundreds of rows:

node_modules/mongoose/lib/utils.js:413
        throw err;
              ^
TypeError: undefined is not a function

I can't see why is this happening. Can anyone help here please?

Regards 

Alejandro de Brito Fontes

unread,
Feb 18, 2014, 1:23:43 PM2/18/14
to nod...@googlegroups.com
You have a callback in the save method?

reda khyatti

unread,
Feb 18, 2014, 5:49:04 PM2/18/14
to nod...@googlegroups.com

Yes I do have a callback

--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
 
---
You received this message because you are subscribed to a topic in the Google Groups "nodejs" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nodejs/v_XTyyO_xV0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nodejs+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

greelgorke

unread,
Feb 19, 2014, 3:01:09 AM2/19/14
to nod...@googlegroups.com
post your code please. looks mostly like a misconseption in the code flow to me

reda khyatti

unread,
Feb 19, 2014, 9:19:16 AM2/19/14
to nod...@googlegroups.com
Hi

So I have this model in /model/Produit.js

var db = require('../lib/db')
, mongoose = require('mongoose');
var ProduitSchema = new db.Schema({
_id : {type:Number},
nom : {type: String},
code : {type: String,unique:true},
prixInitial : {type:Number},
prixReduit : {type:Number},
quantite : {type:Number},
disponible : {type:Boolean},
dateCreation : {type:Date},
cegidDesc : {type:String},
col : {typr:String},
_attribut : {type:mongoose.Schema.Types.ObjectId,ref:'ProduitAttribut'},
_marque : {type:Number,ref:'Marque'}
});

var ProduitClass = db.mongoose.model('Produit',ProduitSchema);

function addNewProduit(nom,code,dateCreation,marque,callback){
var instance = new ProduitClass();
instance.nom = nom;
instance.code = code;
instance.dateCreation = dateCreation;
instance.marque = marque;
instance.save(function(err){
if(err){
console.log('Error '+err);
callback(err)
}
else{
console.log('Saved new product '+code);
callback(null,instance);
}
});
}

module.exports.addNewProduit = addNewProduit;

then I have this file that does the request:

var sql = require('mssql')
  , Produit = require('./models/Produit')
  , ProduitAttribut = require('./models/ProduitAttribut')
  , ProduitAttributValeur = require('./models/ProduitAttributValeur')
  , Marque = require('./models/Marque')
  , _ = require('underscore'); 

var config = {
    user: 'sa',
    password: "***',
    server: 'revsport.no-ip.biz',
    database: 'dbrevsport'
}

var connection = new sql.Connection(config, function(err) {
    if(err){
        console.log('error '+err);
    }

    Marque.findAll(function(err,marques){
        if(err||(!marques)){
            console.log(err);
            throw err;
        }
        else{
            _.each(marques,function(marque){
                console.log('Fetching the '+marque.nom+' products');
                fetchNewProductsOfBrand(marque,function(err,m){
                    if(err){
                        console.log("error here");
                        throw err;
                    }
                });
            });
        }
    });
   
});

function fetchNewProductsOfBrand(marque,callback){
    var request = new sql.Request(connection); // or: var request = connection.request();
    console.log('||Starting the query for the brands product');
    console.log('||At: '+new Date());
    request.query('this is where I put my request', function(err, recordset) {
        if(err){
            console.log('||Errors '+err);
            console.log('||At: '+new Date());
            callback(err);
        }
        else{
            var count = recordset.lenght;
            _.each(recordset,function(article){
                setTimeout(function(){
                    Produit.addNewProduit(article.ga_libelle,article.ga_codearticle,article.ga_datecreation,function(err,m){
                        if(err){
                             console.log('||Errors '+err);
                        }
                    });   
                },50);
            });

        }
        //console.log(recordset);
    });
}



--

Ethan Garofolo

unread,
Feb 20, 2014, 6:24:10 AM2/20/14
to nod...@googlegroups.com
It looks like your addNewProduit expects its callback as the 5th argument, but when you call it in fetchNewProductsOfBrand, you're passing a callback as the 4th argument.  Your invocation is missing the marque.
Reply all
Reply to author
Forward
0 new messages