MongoError: “doc参数必须是一个文档数组”

9

我正在尝试编写一个Angular应用程序,从FullContact API获取JSON格式的信息,并将其插入MongoDB。我可以成功地获得JSON文件,但是当我尝试将其插入MongoDB时,会出现各种错误。我已经解决了其中很多问题,但是卡在这个问题上了。希望得到任何帮助。

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/components/user/user.component.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.component.ts

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

collection.insertMany([veri]).... - sidgate
sidgate - 当我尝试使用你的答案时,出现了以下错误:无法读取未定义的属性 'insertMany'。 - Onur Durmuş
1
db.collection.insertMany([veri] - sidgate
sidgate - 当我尝试使用你的答案时,我遇到了这个错误:db.collection.insertMany不是一个函数。 - Onur Durmuş
不要使用insert,而是使用insertOne - Luiz Soares
显示剩余2条评论
1个回答

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接