This is a javascript code from my node-webkit app, working with jquery and nedb for managing the databases.
librodb.find({_id: docs[i].libro}, function (err, bookdoc) {
window.titulo = bookdoc[0].titulo;
window.ISBN = bookdoc[0].ISBN;
});
That reads the entries from the db and returns them into an array (bookdoc).
for (var i = 0; i < docs.length; i++) {
librodb.find({_id: docs[i].libro}, function (err, bookdoc) {
window.titulo = bookdoc[0].titulo;
window.ISBN = bookdoc[0].ISBN;
});
switch(docs[i].razon){
case 1:
$(".listed").append('<li><i class="fa fa-institution"></i><i class="fa fa-sign-in"></i>El '+docs[i].fecha+' '+docs[i].cantidad+' Libros ("'+window.titulo+'", ISBN: '+window.ISBN+') producidos.</li>');
break;
case 2:
libreriadb.find({_id: docs[i].libreria}, function (err, librarydoc) {
window.nombre = librarydoc[0].nombre;
});
$(".listed").append('<li><i class="fa fa-institution"></i><i class="fa fa-sign-in"></i>El '+docs[i].fecha+' '+docs[i].cantidad+' Libros ("'+window.titulo+'", ISBN: '+window.ISBN+') devueltos por Libreria ("'+window.nombre+'"), recibo '+docs[i].documento+'.</li>');
break;
case 3:
$(".listed").append('<li><i class="fa fa-question"></i><i class="fa fa-sign-in"></i>El '+docs[i].fecha+' '+docs[i].cantidad+' Libros ("'+window.titulo+'", ISBN: '+window.ISBN+') en stock ingresaron por "'+docs[i].descripcion+'".</li>');
break;
}
}
The issue is that the variables window.titulo and window.ISBN are defined inside the reading database function, but outside there arent.
If i use
window.variablename=
When i call the variables after de librodb.find function both return "undefined".
if i use
var variablename=
or
variablename=
The execution stops with the following error: "ReferenceError: titulo is not defined" (in the place where i try to call it from the switch).
In all the three cases an alert inside the librodb.find function returns the value that is supossed to return.
How do i have to define or call the variables?