2016-05-12 14 views
-1

Ich arbeite mit Nodejs + Express und Mongodb.Node + Mongodb + ObjectId funktioniert nicht

Ich benutze Postbote und Zugriff auf die API. Wenn ich ObjectId verwende der Server nichts reagiert. Wenn ich entfernt habe, bedeutet das gut zu funktionieren. Ich kann dieses Problem nicht beheben. Bitte kann mir jemand helfen.

test.js

//Post Data: 
{ 
    "list_id": "56963e4dbcd5d4ff27ced0fbd" 
} 


var app = require('express'); 
var router = app.Router(); 
var server = require('./../../server'); 
var mongoUtil = require('./../../mongoUtil'); 
var ObjectId = require('mongodb').ObjectID; 

router.post('/share', function(req, res, next) { 

    var data = { 
     query : {} 
    }; 
    console.log(req.body['list_id']); 
    //printed 56963e4dbcd5d4ff27ced0fbd 

    console.log(data.query); 
    //printed {} 

    data.query = ObjectId(req.body['list_id']); 
    console.log(data.query); 
    //Here not getting any response 
    // this line not printed and server no response. 
    //Also tried the following things. but its not working. 
    // data.query['_id'] = new ObjectID(req.body['list_id']); 
    //data.query._id = ObjectId(req.body['list_id']); 

    var collection = mongoUtil.list; 
    collection.findOne(data.query, function(err, list) { 
     console.log(err); 
     console.log(list); 
     if (!err && list) { 
      res.send("Sucess"); 
      return; 
     } else { 
      res.send("Error"); 
      return; 
     } 
    }); 

}); 
+0

Welchen Fehler erhalten Sie, wenn Sie ObjectID hinzufügen? – Venky

+0

Ich habe keinen Fehler bekommen. Der Server antwortet nicht. – RSKMR

+0

'ObjectId()' -Konstruktor erwartet eine 24-Byte-Hex-Zeichenfolge, eine 12-Byte-Binär-Zeichenfolge oder eine Zahl als Argument, aber hier ist 'req.body [' list_id '] 'nur eine einfache Zeichenfolge. – chridam

Antwort

1

56963e4dbcd5d4ff27ced0fbd sollte 24 der Länge sein, aber es ist 25. Stellen Sie sicher, dass es 24 ist es funktionieren könnte.

ObjectID() 
Constructor 
Create a new ObjectID instance 

class ObjectID() 
Arguments: 
id (string) – Can be a 24 byte hex string, 12 byte binary string or a Number. 
Returns:  
object instance of ObjectID 
+0

Ich habe versucht, dies funktioniert auch nicht – RSKMR

+0

FWIW, Sie brauchen nicht "neu". – robertklep

+0

@RSKMR hat meine Antwort bearbeitet. Versuche es. – Venky

Verwandte Themen