I use node.js / express and I have Mongodb to store some datasets. On the web page, the user can enter, edit and delete data (everything works fine). For example, to add data, I have the following code:
router.post('/addset', function(req,res) {
var db = req.db;
var collection = db.get('paramlist');
collection.insert(req.body, function(err, result){
res.send(
(err === null) ? { msg: '' } : { msg: err }
);
});
});
In my app.js file I include lines
var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/paramSet1');
and
app.use(function(req,res,next){
req.db = db;
next();
});
to make the database accessible in the rest of the code (following this guide: http://cwbuecheler.com/web/tutorials/2013/node-express-mongo/ , I start with these things).
, . : , . MongoDB, , ? collection.find.limit(1).size(),
undefined is not a function
. (router.post) var...
var findValue = collection.find({name:req.body.name});
console.log(findValue), JSON. console.log(findValue.next()), (undefined ).
collection.find({name:req.body.name}).limit(1)
collection.find({name:req.body.name}).limit(1).size()
. , , collection.insert, collection.update collection.remove , find() . , , .
.
:
console.log(findValue):
{ col:
{ manager:
{ driver: [Object],
helper: [Object],
collections: [Object],
options: [Object],
_events: {} },
driver:
{ _construct_args: [],
_native: [Object],
_emitter: [Object],
_state: 2,
_connect_args: [Object] },
helper: { toObjectID: [Function], isObjectID: [Function], id: [Object] },
name: 'paramlist',
col:
{ _construct_args: [],
_native: [Object],
_emitter: [Object],
_state: 2,
_skin_db: [Object],
_collection_args: [Object],
id: [Object],
emitter: [Object] },
options: {} },
type: 'find',
opts: { fields: {}, safe: true },
domain: null,
_events: { error: [Function], success: [Function] },
_maxListeners: undefined,
emitted: {},
ended: false,
success: [Function],
error: [Function],
complete: [Function],
resolve: [Function],
fulfill: [Function],
reject: [Function],
query: { name: 'TestSet1' } }