首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Node JS Mongodb insert命令

Node JS Mongodb insert命令
EN

Stack Overflow用户
提问于 2019-03-20 03:53:22
回答 2查看 42关注 0票数 0

我目前正在尝试写一个简单的node.js应用程序,使用他们的API和一个围绕它设计的模块从Pokemon TCG网站上抓取数据。我能够从API中获取信息(它将信息存储在一个json样式数组中)。然后,我迭代数组,将所有文档添加到我的机器ATM机上运行的MongoDB容器中。

我的代码如下;

代码语言:javascript
复制
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();
                })
            })
        }
    })
});

当我运行代码时,我得到了以下错误;

代码语言:javascript
复制
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)

我尝试过不同的连接方法。我也尝试添加错误抛出,但错误结果大同小异。

pokemon DB和trainer集合都已创建。但是数据库中没有文档。

我做了一些明显错误的事情

kind关于Elliott

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55249025

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档