最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - MongoError: "doc parameter must be an array of documents" - Stack Overflow

programmeradmin1浏览0评论

Im trying to code an angular app that gets information from fullcontact API as a json and insert that to mongodb.I can successfully get json file but when i try to insert it to mongodb, i get various errors.I solved many of them but im stuck in this one.Any help is appreciated.

ERROR:


  MongoError {name: "MongoError", message: "docs parameter must be an array of documents", driver: true, stack: "MongoError: docs parameter must be an array of doc…(http://localhost:4200/vendor.bundle.js:71114:10)"}driver: truemessage: "docs parameter must be an array of documents"name: "MongoError"stack: "MongoError: docs parameter must be an array of documents↵    at Function.webpackJsonp.../../../../mongodb-core/lib/error.js.MongoError.create (http://localhost:4200/vendor.bundle.js:33916:11)↵    at Collection.webpackJsonp.../../../../mongodb/lib/collection.js.Collection.insertMany (http://localhost:4200/vendor.bundle.js:44864:32)↵    at http://localhost:4200/main.bundle.js:57:20↵    at Db.webpackJsonp.../../../../mongodb/lib/db.js.Db.collection (http://localhost:4200/vendor.bundle.js:49750:20)↵    at Object.webpackJsonp.../../../../../routes/kisiler.js.exports.kisiEkle (http://localhost:4200/main.bundle.js:54:9)↵    at http://localhost:4200/main.bundle.js:366:18↵    at Request.requested [as _callback] (http://localhost:4200/vendor.bundle.js:21598:5)↵    at Request.self.callback (http://localhost:4200/vendor.bundle.js:70137:22)↵    at Request.webpackJsonp.../../../../events/events.js.EventEmitter.emit (http://localhost:4200/vendor.bundle.js:20324:17)↵    at Request.<anonymous> (http://localhost:4200/vendor.bundle.js:71114:10)"__proto__: Error
    at Object.../../../../mongodb-core/lib/error.js (http://localhost:4200/vendor.bundle.js:33927:24)
    at __webpack_require__ (http://localhost:4200/inline.bundle.js:55:30)
    at Object.../../../../mongodb-core/index.js (http://localhost:4200/vendor.bundle.js:28807:17)
    at __webpack_require__ (http://localhost:4200/inline.bundle.js:55:30)
    at Object.../../../../mongodb/index.js (http://localhost:4200/vendor.bundle.js:41002:12)
    at __webpack_require__ (http://localhost:4200/inline.bundle.js:55:30)
    at Object.../../../../../routes/kisiler.js (http://localhost:4200/main.bundle.js:6:13)
    at __webpack_require__ (http://localhost:4200/inline.bundle.js:55:30)
    at Object.../../../../../src/app/ponents/user/userponent.ts (http://localhost:4200/main.bundle.js:349:12)
    at __webpack_require__ (http://localhost:4200/inline.bundle.js:55:30)

//    Kisiler.js 
var mongo = require('mongodb');
var Server = mongo.Server,
Db = mongo.Db,
BSON = mongo.BSONPure;

var server = new Server('localhost', 27017, {auto_reconnect: true});
db = new Db('kisidb', server);

db.open(function(err, db) {
if(!err) {
console.log("Connected to 'kisidb' database");
db.collection('kisiler', {strict:true}, function(err, collection) {
if (err) {
console.log("The 'wines' collection doesn't exist. Creating it with sample data...");
populateDB();
}
});
}
});


exports.kisiEkle = function(veri) {
    db.collection('kisiler', function(err, collection) {
     collection.insertMany(veri, {safe:true}, function(err, result) {
if(err){
console.log(err);
}else {
console.log(result);
}
});
     
});
}

const kisi = require('../../../../routes/kisiler');

// userponent.ts

console.log(data);  // type of data is json.
const veri = JSON.stringify(data); 
kisi.kisiEkle(veri);

Im trying to code an angular app that gets information from fullcontact API as a json and insert that to mongodb.I can successfully get json file but when i try to insert it to mongodb, i get various errors.I solved many of them but im stuck in this one.Any help is appreciated.

ERROR:


  MongoError {name: "MongoError", message: "docs parameter must be an array of documents", driver: true, stack: "MongoError: docs parameter must be an array of doc…(http://localhost:4200/vendor.bundle.js:71114:10)"}driver: truemessage: "docs parameter must be an array of documents"name: "MongoError"stack: "MongoError: docs parameter must be an array of documents↵    at Function.webpackJsonp.../../../../mongodb-core/lib/error.js.MongoError.create (http://localhost:4200/vendor.bundle.js:33916:11)↵    at Collection.webpackJsonp.../../../../mongodb/lib/collection.js.Collection.insertMany (http://localhost:4200/vendor.bundle.js:44864:32)↵    at http://localhost:4200/main.bundle.js:57:20↵    at Db.webpackJsonp.../../../../mongodb/lib/db.js.Db.collection (http://localhost:4200/vendor.bundle.js:49750:20)↵    at Object.webpackJsonp.../../../../../routes/kisiler.js.exports.kisiEkle (http://localhost:4200/main.bundle.js:54:9)↵    at http://localhost:4200/main.bundle.js:366:18↵    at Request.requested [as _callback] (http://localhost:4200/vendor.bundle.js:21598:5)↵    at Request.self.callback (http://localhost:4200/vendor.bundle.js:70137:22)↵    at Request.webpackJsonp.../../../../events/events.js.EventEmitter.emit (http://localhost:4200/vendor.bundle.js:20324:17)↵    at Request.<anonymous> (http://localhost:4200/vendor.bundle.js:71114:10)"__proto__: Error
    at Object.../../../../mongodb-core/lib/error.js (http://localhost:4200/vendor.bundle.js:33927:24)
    at __webpack_require__ (http://localhost:4200/inline.bundle.js:55:30)
    at Object.../../../../mongodb-core/index.js (http://localhost:4200/vendor.bundle.js:28807:17)
    at __webpack_require__ (http://localhost:4200/inline.bundle.js:55:30)
    at Object.../../../../mongodb/index.js (http://localhost:4200/vendor.bundle.js:41002:12)
    at __webpack_require__ (http://localhost:4200/inline.bundle.js:55:30)
    at Object.../../../../../routes/kisiler.js (http://localhost:4200/main.bundle.js:6:13)
    at __webpack_require__ (http://localhost:4200/inline.bundle.js:55:30)
    at Object.../../../../../src/app/ponents/user/user.ponent.ts (http://localhost:4200/main.bundle.js:349:12)
    at __webpack_require__ (http://localhost:4200/inline.bundle.js:55:30)

//    Kisiler.js 
var mongo = require('mongodb');
var Server = mongo.Server,
Db = mongo.Db,
BSON = mongo.BSONPure;

var server = new Server('localhost', 27017, {auto_reconnect: true});
db = new Db('kisidb', server);

db.open(function(err, db) {
if(!err) {
console.log("Connected to 'kisidb' database");
db.collection('kisiler', {strict:true}, function(err, collection) {
if (err) {
console.log("The 'wines' collection doesn't exist. Creating it with sample data...");
populateDB();
}
});
}
});


exports.kisiEkle = function(veri) {
    db.collection('kisiler', function(err, collection) {
     collection.insertMany(veri, {safe:true}, function(err, result) {
if(err){
console.log(err);
}else {
console.log(result);
}
});
     
});
}

const kisi = require('../../../../routes/kisiler');

// user.ponent.ts

console.log(data);  // type of data is json.
const veri = JSON.stringify(data); 
kisi.kisiEkle(veri);
Share Improve this question edited Sep 27, 2017 at 10:50 sidgate 15.3k12 gold badges75 silver badges131 bronze badges asked Sep 27, 2017 at 10:41 Onur DurmuşOnur Durmuş 1421 gold badge1 silver badge7 bronze badges 7
  • collection.insertMany([veri].... – sidgate Commented Sep 27, 2017 at 10:52
  • sidgate - when I tried to your answer , I get this error : Cannot read property 'insertMany' of undefined – Onur Durmuş Commented Sep 27, 2017 at 11:02
  • 1 db.collection.insertMany([veri] – sidgate Commented Sep 27, 2017 at 11:13
  • sidgate - when I tried to your answer , I get this error : db.collection.insertMany is not a function – Onur Durmuş Commented Sep 27, 2017 at 11:21
  • Instead of collection.insertMany(veri, {safe:true}, function(err, result) { try this collection.insert(veri, {safe:true}, function(err, result) { – dnickless Commented Sep 27, 2017 at 19:54
 |  Show 2 more ments

1 Answer 1

Reset to default 0

As far as I understand it MongoClient returns a client object so you'll need client.db.collection

see this db.collection is not a function when using MongoClient v3.0

发布评论

评论列表(0)

  1. 暂无评论