javascript - 蒙哥错误: "doc parameter must be an array of documents"

标签 javascript json mongodb fullcontact

我正在尝试编写一个 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);

最佳答案

据我了解,MongoClient 返回一个客户端对象,因此您需要 client.db.collection

看这个db.collection is not a function when using MongoClient v3.0

关于javascript - 蒙哥错误: "doc parameter must be an array of documents",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46445796/

相关文章:

javascript - Synpress 无法使用 cy.acceptMetamaskAccess (不是函数)

javascript - 将多个对象传递给 angularjs $resource.save()

php - 使用 PHP 将 mySQL 表转换为带有子数组的 JSON 数组

javascript - 比较两个数组并删除不匹配的对象

javascript - 条件验证问题?

javascript - 用于查看驱动器中任何文件的网络应用程序链接

json - Golang JSON Unmarshal 序列号

mongodb - 在 MongoDB 中执行搜索/投影时如何重命名字段?

mongodb - 使用ELK堆栈将用户信息添加到集中式日志记录中

mysql - 使用循环在 mongoDB 中查询