Error to use mongodb filter

Asked

Viewed 40 times

0

I have an error trying to run a filter in the database.

var params = '{"cd_entidade":"'+ vcd_entidade+'"}';

Movmaterial.find(params).toArray(function(err,docs){
    if (err) throw err;
    console.log(docs);
}

an error has occurred that I am unable to resolve, it already has two days :( could you help me? I am creating a personal project for studies of the use of mongodb, I thank you

  • 1

    What’s wrong with you?

  • find(...). toArray is not a Function at Object.index

  • Movmaterial is your mongodb? The return is probably not valid for not being able to call the toArray, debug what the find returns. Or try doing find({}).toArray.

  • thanks for the interest, come on..

  • Movmaterial is mongodb and works when I do it as follows: Movmaterial.find({cd_entity:vcd_entity},Function(err,Docs){.. when I put inside a variable does not work. this is just an example, as I will include more arguments to better filter the data.

  • I have tried to put it in the suggested way and the error it generated was: Typeerror: Movmaterial.find(...). toArray is not a Function at Object.index

  • so that I have sent now, no error, you have selected all Collection data. The problem is that you didn’t filter the data, processed all the documents in the console.log.

Show 2 more comments

1 answer

0

Your params is a string, uses a normal javascript object.

// aqui nao precisas contatenar como estavas a fazer, isto será suficiente
var params = { cd_entidade: vcd_entidade };

Movmaterial.find(params).toArray(function(err,docs){
    if (err) throw err;
    console.log(docs);
}
  • I’ll try and return then.

  • did as suggested and returned the same error:Typeerror: Movmaterial.find(...). toArray is not a Function :(

  • What comes back if you only use the find? Movmaterial is the collection or a db?

  • i am using as Collection. in the initial file definition and create a variable: var Movmaterial = app.models.movmaterial;

  • where the movmaterial is Collection. in the model folder follows the model definition: module.Exports = Function() { var movmaterialSchema = Mongoose.Schema({ data_input : {type: Date, default: Date}, cd_entity : {type: String, Trim: true}, cd_nro_nf : {type: String, Trim: true}, id_active : {type: String, Trim: true}, ds_ativmaterial : {type: String, Trim: true} }); Return Mongoose.model('Movmaterial', movmaterialSchema); }

  • And only the find? Because the .toArray cannot be called due to the return of find

  • i tried this way: Movmaterial.find(params,Function(err,Docs){..... returned the data but did not apply the filter, generated all Collection data.

  • did not generate an error, but did not filter the data. see how it looked. var params = { cd_entity: vcd_entity } Movmaterial.find(params,Function(err,Docs){ if (err){ req.flash('error','Error : '+err); Docs = []; res.render('movmaterial/index',{list: Docs,cd_entity:req.params.cd_entity,entity:dbent,material:dbtmat}); }Else { console.log(Docs); res.render('movmaterial/index',{list: Docs,cd_entity:req.params.cd_entity,entity:dbent,material:dbtmat}); } });

  • the filter information, if applied would not select data to list, the information in the filter does not exist, I get the impression that the filter was not considered as argument to filter in the database.

  • Consgui reoslver....:)

  • I was able to solve the problem

  • I didn’t need to use toArray, as you reported @Leite. worked: var params = Object({ cd_entity: vcd_entity}); console.log(params) Movmaterial.find(params,Function(err,Docs){ if (err){ req.flash('error','Error : '+err); Docs = []; res.render('movmaterial/index',{list: Docs}); }Else { console.log(Docs); res.render('movmaterial/index',{list: Docs}); } });

  • 1

    define param as follows: var params = Object({ cd_entity: vcd_entity}); where vcd_entity is a var that will be included the arguments for filtering. Grateful for the help and the tips, they helped me a lot. Grateful.

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.