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);
});
}
On Wed, Feb 19, 2014 at 8:01 AM, greelgorke <[email protected]> wrote:
> post your code please. looks mostly like a misconseption in the code flow
> to me
>
> Am Dienstag, 18. Februar 2014 15:40:46 UTC+1 schrieb reda khyatti:
>
>> 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
>>
> --
> --
> 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 [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> 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
> [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>
--
--
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
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 the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.