我目前正在尝试编写一个简单的node.js应用程序,使用他们的API和一个围绕它设计的模块从Pokemon TCG网站获取数据。我能够从API获取信息(这将其存储在一个json风格的数组中)。然后我迭代数组,将所有文档添加到我在机器atm上运行的MongoDB容器中。
我的代码如下:
const pokemon = require('pokemontcgsdk');
var prompt = require('prompt');
var mongo = require('mongodb');
var databaseconnect = 'mongo://localhost:27017/';
prompt.start();
prompt.get(['setcodein'], function(err, result) {
console.log(result.setcodein);
var settosearch = result.setcodein;
pokemon.card.where({ supertype: 'Trainer', setCode: settosearch })
.then(results => {
for(i =0;i < results.length;i++){
console.log(results[i].name);
mongo.connect(databaseconnect, function(err, db){
var dbo = db.db("pokemon");
dbo.collection("trainer").insertOne(result[i], function(err, r){
console.log('Card Added to database');
db.close();
})
})
}
})
});
当我运行这段代码时,我得到了以下错误;
prompt: setcodein: sm9
sm9
Bill's Analysis
(node:5090) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option {
useNewUrlParser: true } to MongoClient.connect.
Unhandled rejection TypeError: Cannot read property 'db' of null
at /Users/elliottbuckingham/Google Drive/Coding/Java/Pokemon App/index.js:19:30
at err (/Users/elliottbuckingham/Google Drive/Coding/Java/Pokemon App/node_modules/mongodb/lib/utils.js:415:14)
at executeCallback (/Users/elliottbuckingham/Google Drive/Coding/Java/Pokemon App/node_modules/mongodb/lib/utils.js:404:25)
at executeOperation (/Users/elliottbuckingham/Google Drive/Coding/Java/Pokemon App/node_modules/mongodb/lib/utils.js:422:7)
at MongoClient.connect (/Users/elliottbuckingham/Google Drive/Coding/Java/Pokemon App/node_modules/mongodb/lib/mongo_client.js:168:10)
at Function.MongoClient.connect (/Users/elliottbuckingham/Google Drive/Coding/Java/Pokemon App/node_modules/mongodb/lib/mongo_client.js:372:22)
at pokemon.card.where.then.results (/Users/elliottbuckingham/Google Drive/Coding/Java/Pokemon App/index.js:18:19)
at tryCatcher (/Users/elliottbuckingham/Google Drive/Coding/Java/Pokemon App/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/Users/elliottbuckingham/Google Drive/Coding/Java/Pokemon App/node_modules/bluebird/js/release/promise.js:51
2:31)
at Promise._settlePromise (/Users/elliottbuckingham/Google Drive/Coding/Java/Pokemon App/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/Users/elliottbuckingham/Google Drive/Coding/Java/Pokemon App/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/Users/elliottbuckingham/Google Drive/Coding/Java/Pokemon App/node_modules/bluebird/js/release/promise.js:694:18)
at _drainQueueStep (/Users/elliottbuckingham/Google Drive/Coding/Java/Pokemon App/node_modules/bluebird/js/release/async.js:138:12)
at _drainQueue (/Users/elliottbuckingham/Google Drive/Coding/Java/Pokemon App/node_modules/bluebird/js/release/async.js:131:9)
at Async._drainQueues (/Users/elliottbuckingham/Google Drive/Coding/Java/Pokemon App/node_modules/bluebird/js/release/async.js:147:5)
at Immediate.Async.drainQueues [as _onImmediate] (/Users/elliottbuckingham/Google Drive/Coding/Java/Pokemon App/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)
我试过不同的连接方法。我也试过添加err throw,但错误出来或多或少是一样的。
口袋妖怪数据库和训练器集合都创建好了。但是数据库里没有文档。
我做了件很明显的错事
亲切问候Elliott
3条答案
按热度按时间ct3nt3jp1#
检查您的mongo版本
如果您使用的版本〉= 3.1.0,请将mongo连接文件更改为-〉
理想情况下,这是一个版本4的特性,但是v3.1.0和更高版本也支持它。
am46iovg2#
谢谢大家。我已经厌倦了从github重写代码,然后还使用
声明。
这已经解决了我的问题。亲切的问候
6uxekuva3#
连接mongodb
mongoose.connect(蒙哥数据库://本地主机:27017,{使用新URL解析器:true,使用统一拓扑:真});