【问题标题】:insert data to external mongodb with meteor app使用流星应用程序将数据插入外部 mongodb
【发布时间】:2017-05-05 09:45:04
【问题描述】:

我在服务器中有一个 mongodb 的瞬间,我使用该代码将我的流星应用程序连接到该数据库:lib/connection.js

 MONGO_URL = 'mongodb://xxxxxxxx';
   var mongoClient = require("mongodb").MongoClient;
   mongoClient.connect(MONGO_URL, function (err, db) {

    if (err) {
            console.log('Unable to connect to the mongoDB server. Error:', err);
        } else {
            console.log('Connection established to cc', MONGO_URL);


             var collection = db.collection('test');
             var test1= {'hello':'test1'};
             collection.insert(test1);      
             db.close();
       }

    });

与外部 mongo 的连接已建立,并且在服务器中创建了集合测试,但是当我插入我的集合时,我的应用程序仍然连接到本地 mongo:书籍:

你的代码:collections/Books.js

Books= new Mongo.Collection('books');

BooksSchema= new SimpleSchema({

  name: {
    type: String,
    label: "Name"
    autoform:{
     label: false,
      placeholder: "schemaLabel"
        }
  },
  categorie:{
    type: String,
    label: "Categorie"
    autoform:{
     label: false,
      placeholder: "schemaLabel"
        }
  },


});

Meteor.methods({
deleteBook: function(id) {
  Cultures.remove(id);
}

});

Books.attachSchema(BooksSchema);

代码客户端/books.html

 <template name="books">
    <p>add new books </p>
    {{> quickForm collection="Books" id="newBook" type="insert" class="nform" buttonContent='ajouter' buttonClasses='btn bg-orange'}}
    </template>

帮助 bleaaaaaz

【问题讨论】:

  • 如何修改我的代码以使数据插入到外部数据库中

标签: mongodb meteor insert external


【解决方案1】:

您应该在MONGO_URL 环境变量中指定应该使用的数据库,而不是在您的代码中。如果您在本地工作,请像这样启动您的应用程序:

MONGO_URL="mongodb://xxxxxxxx" meteor

UPD

不了解 Windows。看到这个SO question

看起来你应该像这样在窗口中设置环境变量:

set MONGO_URL=mongodb://localhost:27017/mydbname

【讨论】:

  • 谢谢你的回答,我试过了,但我在 Windows 中工作,MOGO_URL 没有工作,我想在 server/main.js 中制作该代码 ********** ****** Meteor.startup(() => { console.log(process.env.MONGO_URL); process.env['MONGO_URL'] = 'mongodb://xxxxxxxx'; console.log(process.env .MONGO_URL); });**************
  • witch的意思是修改了环境变量但是数据还是插入到了本地的mongodb中
【解决方案2】:

好的,谢谢 Ramil,我在 windows 上创建了一个新的系统环境变量,MOGO_URL 的值等于:mongodb://xxxxxxxx,它可以工作;应用程序连接到服务器中的数据库,并将数据插入其中。 现在我的问题是如何从该数据库中获取数据,我使用 Microsoft azure 来存储带有 API DocumentDB 的数据库

【讨论】:

  • 我想这是另一个问题的问题,因为您解决了连接问题。祝你好运!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-28
  • 1970-01-01
  • 2014-08-28
  • 2013-02-28
  • 2012-12-19
  • 2023-03-28
  • 1970-01-01
相关资源
最近更新 更多