0

Using the code below I want to find the value in the field seq for the row where _id=user:

DbModels.prototype.autoIncrement = function(_collection, callback){
  this.getCollection(_collection, function(error, seq_collection) {
  if( error ) callback(error)
      else {
        console.log(seq_collection);
        seq_collection.find({_id: 'Users'}, {'seq': 1}, function(error, result) {
                if( error ) callback(error)
                else {callback(null, result)}
              });
    }
  });
}

DbModels.prototype.addUser = function(Users, callback) { 

  this.autoIncrement("seq", function(error, result){
    if(error) callback(error);
                else console.log("RESULT: "+result);ID=result;
  });

The code is working properly, however the results is "RESULT: [object Object]" and I have no idea how I can convert [object Object] to a value in nodejs.

When I run the query in the mongo shell the output is: {"_id":"Users","seq":51}

Anyone any suggestions?

The Code Buccaneer
  • 775
  • 1
  • 10
  • 21

3 Answers3

2

It is a value (specifically a reference to an object). It only looks like that when you call toString (implicitly because of the concatenation). You probably want to look at the docs and see what the relevant fields and methods are for result.

From these docs, it looks like you might want to process the result a little differently:

seq_collection.find({_id: 'Users'}, {'seq': 1}).toArray(function(err, results){
    console.log(results); // output all records
});
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
1

I think, this is the answer: How do you log content of a JSON object in Node.js?

The easiest way is:

this.autoIncrement("seq", function(error, result){
    if(error) callback(error);
              else console.log("RESULT: %j",result);ID=JSON.stringify(result);
});
Community
  • 1
  • 1
user870635
  • 11
  • 1
  • seems that has already been suggested. also i think the OP wants the `seq` field so `ID` should probably set to that specifically instead of the whole result object. – olly_uk Apr 25 '13 at 09:05
1

Try:

console.log("RESULT: "+JSON.stringify(result));

It should print the contents of the result.

On response to the comment (missing formatting in comment):

Try this in a node console:

> o = {a:13}
{ a: 13 }
> o['c'] = o
{ a: 13, c: [Circular] }
> JSON.stringify(o)
TypeError: Converting circular structure to JSON
    at Object.stringify (native)
    at repl:1:7
    at REPLServer.eval (repl.js:80:21)
    at Interface.<anonymous> (repl.js:182:12)
    at Interface.emit (events.js:67:17)
    at Interface._onLine (readline.js:162:10)
    at Interface._line (readline.js:426:8)
    at Interface._ttyWrite (readline.js:603:14)
    at ReadStream.<anonymous> (readline.js:82:12)
    at ReadStream.emit (events.js:88:20)
torbenl
  • 296
  • 2
  • 6
  • That gives the following result: TypeError: Converting circular structure to JSON at Object.stringify (native) at c:\Program Files\nodejs\DbModels.js:192:50 at c:\Program Files\nodejs\DbModels.js:182:23 at Collection.find (c:\Program Files\nodejs\node_modules\mongodb\lib\mongodb\collection.js:698:5) at c:\Program Files\nodejs\DbModels.js:180:24 at c:\Program Files\nodejs\DbModels.js:63:10 at [object Object].collection (c:\Program Files\nodejs\node_modules\mongodb\lib\mongodb\db.js:320:44) at [object Object].getCollection (c:\Program Files\nodejs\DbModels.js... – The Code Buccaneer Feb 27 '12 at 21:51
  • 1
    It looks like you have a circular reference in your data. I have updated the answer - to get the formatting. – torbenl Feb 27 '12 at 22:10
  • I'm quite certain OP doesn't grok what that means torben. – jcolebrand Feb 27 '12 at 22:14
  • @torbenl, `results` clearly isn't pure data. It has self-references, and probably methods too. So it's not appropriate for either `stringify` or `toString`. – Matthew Flaschen Feb 27 '12 at 22:27
  • Maybe you could try to do the find in the 'mongo' shell? – torbenl Feb 27 '12 at 22:33
  • @torbenl, presumably he'll have similar issues when he needs to do something real. He needs to find a way to query where he can understand the data model of the response. As is, this doesn't answer the question. – Matthew Flaschen Feb 27 '12 at 22:37
  • That was exactly my point - try to do the `find` in a 'mongo' shell, to see what constitutes the response. – torbenl Feb 27 '12 at 22:41
  • The output in the mongo shell is: {"_id":"Users","seq":51} – The Code Buccaneer Mar 01 '12 at 21:18
  • Take a look at one of the threads about printing a JavaScript object e.g. [Print content of JavaScript object?](http://stackoverflow.com/questions/1625208/print-content-of-javascript-object) – torbenl Mar 28 '12 at 08:16