The proper way to do this (i think) is to pass a function as the last
parameter of your find function, then when you have the response from
Mongo, call that function and pass the results to it, along with any
possible error.
function list(callback) {
var serverMongo = new mongodb.Server("127.0.0.1", 27017, {});
new mongodb.Db('mytestnode', serverMongo, {}).open(function (error,
client) {
if (error) throw error;
var collection = new
mongodb.Collection(client, 'mytestcollection');
collection.find().toArray( function (err, objects) {
if (err) {
console.warn(err.message);
callback(err, objects);
} else {
console.log(objects);
callback(null, objects);
}
serverMongo.close();
});
});
}
On Monday, March 5, 2012 11:28:46 AM UTC-5, AndDM wrote:
>
> Hi, I'm a new nodejs's and javascript's user, i wrote this function
> for test that get data form a MongoDB's collection:
> function list() {
> var serverMongo = new mongodb.Server("127.0.0.1", 27017, {});
> new mongodb.Db('mytestnode', serverMongo, {}).open(function
> (error, client) {
> if (error) throw error;
> var collection = new mongodb.Collection(client,
> 'mytestcollection');
> collection.find().toArray( function (err, objects) {
> if (err) {
> console.warn(err.message);
> } else {
> console.log(objects);
> }
> serverMongo.close();
> });
> });
> }
> I call it as a module and i should pass the result to a variable:
> client_list = status.list();
> I'm trying to set return in all part of the code, without result.
> Could you tell some help with this problem?
>
> Thanks for your time.
> Best regards.
On Monday, March 5, 2012 11:28:46 AM UTC-5, AndDM wrote:
>
> Hi, I'm a new nodejs's and javascript's user, i wrote this function
> for test that get data form a MongoDB's collection:
> function list() {
> var serverMongo = new mongodb.Server("127.0.0.1", 27017, {});
> new mongodb.Db('mytestnode', serverMongo, {}).open(function
> (error, client) {
> if (error) throw error;
> var collection = new mongodb.Collection(client,
> 'mytestcollection');
> collection.find().toArray( function (err, objects) {
> if (err) {
> console.warn(err.message);
> } else {
> console.log(objects);
> }
> serverMongo.close();
> });
> });
> }
> I call it as a module and i should pass the result to a variable:
> client_list = status.list();
> I'm trying to set return in all part of the code, without result.
> Could you tell some help with this problem?
>
> Thanks for your time.
> Best regards.
On Monday, March 5, 2012 11:28:46 AM UTC-5, AndDM wrote:
>
> Hi, I'm a new nodejs's and javascript's user, i wrote this function
> for test that get data form a MongoDB's collection:
> function list() {
> var serverMongo = new mongodb.Server("127.0.0.1", 27017, {});
> new mongodb.Db('mytestnode', serverMongo, {}).open(function
> (error, client) {
> if (error) throw error;
> var collection = new mongodb.Collection(client,
> 'mytestcollection');
> collection.find().toArray( function (err, objects) {
> if (err) {
> console.warn(err.message);
> } else {
> console.log(objects);
> }
> serverMongo.close();
> });
> });
> }
> I call it as a module and i should pass the result to a variable:
> client_list = status.list();
> I'm trying to set return in all part of the code, without result.
> Could you tell some help with this problem?
>
> Thanks for your time.
> Best 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