Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Can't return a string properly with mongoskin

Can't return a string properly with mongoskin

Problem

So I have this script:

var db = require('mongoskin').db('localhost:27017/titles', {safe:true});
var titles = db.collection('titles');

title = titles.findOne(function(err, result){
if (err) throw err;
return result.title;
});

console.log(title)

And this BSON Object in the collection:

{"title": "Hello World", "postNumber": 0, "_id": ObjectId("509eeffbf8f11e8813000001")}

My goal is got get the string "Hello World" to output to the console, but instead I get this:

{ emitter: { _events: { open: [Object] }, _maxListeners: 50 },
  state: 1,
  options: undefined,
  skinDb:
   { emitter: { _events: [Object], _maxListeners: 100 },
     state: 1,
     _dbconn:
      { databaseName: 'titles',
    serverConfig: [Object],
    options: [Object],
    _applicationClosed: false,
    native_parser: true,
    bsonLib: [Object],
    bson: {},
    bson_deserializer: [Object],
    bson_serializer: [Object],
    _state: 'connecting',
    pkFactory: [Object],
    forceServerObjectId: false,
    safe: true,
    notReplied: {},
    isInitializing: true,
    auths: [],
    openCalled: true,
    commands: [],
    _callBackStore: [Object],
    logger: [Object],
    slaveOk: false,
    tag: 1352682584657,
    eventHandlers: [Object],
    serializeFunctions: false,
    raw: false,
    recordQueryStats: false,
    reaperEnabled: false,
    _lastReaperTimestamp: 1352682584657,
    retryMiliSeconds: 1000,
    numberOfRetries: 60,
    reaperInterval: 10000,
    reaperTimeout: 30000,
    readPreference: undefined },
 db: null,
 username: '',
 password: undefined,
 admin: { emitter: {}, state: 0, skinDb: [Circular], admin: null },
 _collections: { titles: [Circular] },
 bson_serializer:
  { BSON: [Object],
    Long: [Object],
    ObjectID: [Object],
    DBRef: [Function: DBRef],
    Code: [Function: Code],
    Timestamp: [Object],
    Binary: [Object],
    Double: [Function: Double],
    MaxKey: [Function: MaxKey],
    MinKey: [Function: MinKey],
    Symbol: [Function: Symbol] },
 ObjectID:
  { [Function: ObjectID]
    index: 0,
    createPk: [Function: createPk],
    createFromTime: [Function: createFromTime],
    createFromHexString: [Function: createFromHexString] } },
  ObjectID:
   { [Function: ObjectID]
 index: 0,
 createPk: [Function: createPk],
 createFromTime: [Function: createFromTime],
 createFromHexString: [Function: createFromHexString] },
  collectionName: 'titles',
  collection: null,
  internalHint: null,
  hint: [Getter/Setter] }

It works just fine when I put the console.log() inside the Function, I just can't return the string for some reason.

Solved:

read = function(callback){
    titles.findOne(function(err, result){
        if (err) throw err;
        callback(result.title);
    });
};

read(function(title){
console.log(title);
});
Problem courtesy of: user1816679

Solution

you need to understand javascript callback.

1 - findOne will return a db object not the result of your string. I never read the mongoskin doco but I think it will be a db collection or some sort which is what you see in the console.log print out. 2 - in the callback your do return result.title; will never return the title back as you would expect. so that is why I suggest to do some reading on how callback work.

The correct way to print the title will be putting the console.log inside the callback function as you mention.

Solution courtesy of: wayne

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Can't return a string properly with mongoskin

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×