【问题标题】:Node JS Mongodb insert commandNode JS Mongodb 插入命令
【发布时间】:2019-03-22 17:13:18
【问题描述】:

我目前正在尝试编写一个简单的 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)

我尝试了不同的连接方法。我也尝试添加错误抛出,但错误或多或少相同。

口袋妖怪数据库和训练师集合都已创建。但是数据库中没有文档。

我做错了事

亲切的问候 艾略特

【问题讨论】:

标签: arrays node.js json mongodb


【解决方案1】:

检查你的 mongo 版本

mongo --version

如果您使用的是 >= 3.1.0 版本,请将您的 mongo 连接文件更改为 ->

MongoClient.connect("mongodb://localhost:27017/YourDB", { useNewUrlParser: true })
or your mongoose connection file to ->

mongoose.connect("mongodb://localhost:27017/YourDB", { useNewUrlParser: true });

理想情况下,它是第 4 版功能,但 v3.1.0 及更高版本也支持它。详情请查看 MongoDB Github。

【讨论】:

    【解决方案2】:

    谢谢大家。我已经厌倦了从 github 重写代码然后还使用

     { useNewUrlParser: true })
    

    声明。

    这解决了我的问题。亲切的问候

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-18
      • 2017-10-17
      • 1970-01-01
      • 1970-01-01
      • 2019-10-09
      • 2016-11-27
      • 2013-03-10
      相关资源
      最近更新 更多