db使用`Meteor Js`在`mongoDB`中未定义错误

时间:2014-01-24 04:52:28

标签: meteor

下面是我的示例代码,如果运行此代码 db未定义错误 是如何解决此错误的。但我紧张的是用户详细信息存储在提交表单后mongoDb。并发送mongodb命令提示操作.Plz帮我任何身体......

user = new Meteor.Collection('test');

//Metro Client

if (Meteor.isClient) {

  Template.hello.events({
    'submit #loginform' : function (e , t) 
{
        e.preventDefault();
      var username = t.find('#username').value;
          var password = t.find('#password').value;
      var email=t.find('#email').value;

   //Insert values Into mongodb
          user.insert
        ({
          username:username,
          password: password,
      email:email,

        });
    j = { name : "username" }
        db.test.insert( j );
    console.log("username="+username);
        console.log("password="+password);
    console.log("email="+email);
    }
  });
 // Send email is client
 Meteor.call('sendEmail',
            'xxxx@.com',
            'yyyy@gmail.com',
            'Hello from Meteor!');

}

//Metro Server

if (Meteor.isServer) {
  Meteor.startup(function () {

  });

 // Send email is Server

      Meteor.methods({
      sendEmail: function (to, from, subject, text) {
        //console.log("email="+to);
         process.env.MAIL_URL =      'smtp://AAAA@gmail.com:****@smtp.gmail.com:587'; 

        check([to, from, subject, text], [String]);
        this.unblock();
        Email.send({
          to: to,
          from: from,
          subject: subject,
          text: text
        });
      }
    });


}

1 个答案:

答案 0 :(得分:3)

在mongo shell中,你会插入一个名为'test'的集合,如下所示:

db.test.insert(...);

在meteor中,您将插入一个定义如下的集合:

Test = new Meteor.Collection('test');

使用如下命令:

Test.insert(...);

您的代码假定对象db已定义,就像在mongodb shell中一样。事实并非如此。您需要将行更改为:

user.insert(j);

使用meteor时,您无法从代码中发出mongo shell命令 - 您只能对Test等集合对象执行操作。然后,这些更改将反映在您的数据库中。当meteor正在运行时,您可以通过在项目的根目录中打开一个新终端并输入以下内容来查看:

$ meteor mongo

这将打开一个mongo shell,您可以直接访问数据,如:

db.test.findOne();

请注意,如果您使用meteor存储用户和密码,则应使用内置的accounts-password包。