Good day!

There is a chain of modules that forms the query string in the database, makes this query and gives an answer. I wrote the problem in the comments to the code. I think it will be so clearer.

function findInDb(data, callback){ //функция Ρ„ΠΎΡ€ΠΌΠΈΡ€ΡƒΠ΅Ρ‚ ΠΈ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ строку запроса для Π‘Π” formQuerystring(data, (query) =>{ //посСл получСния строки запроса ΠΎΡ‚ΠΊΡ€Ρ‹Π²Π°ΡŽ ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ ΠΊ Π‘Π” mongodb.connect((err, client)=>{ var db = client.db("goods"); //ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π°ΡŽΡΡŒ ΠΊ ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΠΈ db.collection(collectionName,(err, collection) =>{ сonsole.log(query) //{"$and":[{"visible":true},{"article":{"$in":[16]}}]} //Π·Π°ΠΏΡƒΡΠΊΠ°ΡŽ поиск Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚ΠΎΠ² ΡΠΎΠΎΡ‚Π²Π΅Ρ‚ΡΡ‚Π²ΡƒΡŽΡ‰ΠΈΡ… ΠΊΡ€ΠΈΡ‚Π΅Ρ€ΠΈΡŽ поиска collection.find(query).sort(sortValue).skip(skip).limit(limit).toArray((err, result)=>{ //Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎ ΡƒΠ·Π½Π°Ρ‚ΡŒ ΠΊΠΎΠ»-Π²ΠΎ Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚ΠΎΠ² ΡΠΎΠΎΡ‚Π²Π΅Ρ‚ΡΡ‚Π²ΡƒΡŽΡ‰ΠΈΡ… ΠΊΡ€ΠΈΡ‚Π΅Ρ€ΠΈΡŽ поиска (для ΠΏΠ°Π³ΠΈΠ½Π°Ρ†ΠΈΠΈ) //ΠΏΡ€ΠΎΠ±Π»Π΅ΠΌΠ° здСсь! console.log(query) // Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ undefined collection.count(query,(err, count) =>{ var f ={}; f.result = result; f.count = count; client.close(); return callback(f); }) }) }) }) }) } 

Why query return - undefined ?

Version NodeJs - 8.9.4

MongoDB - 3.0

MongoDD module for NodeJS - 3.0

  • сonsole.log (query) // {"$ and": [{"visible": true}, {"article": {"$ in": [16]}}]} but here it always displays correctly? - Grundy
  • console.log(query) - why so deep? If query is undefined , then this can be found out already before the comment //посСл ... there some connection between what is in data and the undefined query ? - Igor
  • @Grundy, yes, there is always true - Dmytryk
  • @Igor, did not quite understand the connection between the location of the comment and the variable. Comments for the question are written. There is a connection between data and query , depending on the fact that the query is also formed in data . But the entry into the formQuerystring function happens 1 time. And I do not change the query anywhere, I only use it when accessing the database. What makes me up is that the problem occurs periodically. - Dmytryk
  • An object cannot become undefined just like that. if at the higher level the value is always correct, and at the lower level - undefined means either not all the code is provided or another local variable is used or a parameter with the same name - Grundy

0