I am creating an sample library app in nodejs+express+mongodb. when I run the gulp serve command the server start perfectly. when I goto specific mongodb page then it giving error insertMany is not a function.
var express = require('express');
var adminRouter = express.Router();
var mongodb = require('mongodb').MongoClient;
var books = [
{
title: 'world',
genre: 'Lorem Ipsum text changing occured',
author: 'Rakesh',
read: false
},
{
title: 'Make of the world',
genre: 'Digital India towards occured',
author: 'Jumanji',
read: false
}
];
var router = function (nav) {
adminRouter.route('/addBooks')
.get(function (req, res) {
var url = 'mongodb://127.0.0.1:27017/admin';
mongodb.connect(url, function (err, db) {
var collections = db.collections('books');
collections.insertMany(books,
function (err, results) {
res.send(results);
db.close();
}
);
});
});
return adminRouter;
};
module.exports = router;
There is a typo in syntax in line var collections = db.collections('books');
. The node mongo driver could only interpret a db.collection() function not db.collections() which actually is syntactically wrong.
Use db.collection('books')
instead of db.collections('books')
, this should fix the bug. Try it and let me know
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.