2012-06-13 44 views
1

当我开始我的JS文件,我得到了如下错误:错误的节点和MongoDB

Charon:modules Modius$ node testfile.js 

node.js:201 
     throw e; // process.nextTick error, or 'error' event on first tick 
      ^
ReferenceError: define is not defined 
    at Object.<anonymous> (/Applications/MAMP/htdocs/spacebattles/server/modules/testfile.js:11:1) 
    at Module._compile (module.js:441:26) 
    at Object..js (module.js:459:10) 
    at Module.load (module.js:348:31) 
    at Function._load (module.js:308:12) 
    at Array.0 (module.js:479:10) 
    at EventEmitter._tickCallback (node.js:192:40) 
Charon:modules Modius$ 

,这里的js文件

define(['./db_user'], function (db_user) { 

    var db_user = document.createElement("db_user.js"); 
    var tranfer = { 
     login: "test" , 
     email: "holishitumsonst.com", 
     password: "123nu" 
    }; 
    db_user.insert(transfer); 

}); 

感谢您的帮助

回答

1

我想你可能会误解节点的概念。像弗洛里安说的那样,node.js是“仅”V8引擎,没有围绕它的浏览器。这意味着你没有文档(根本没有DOM)。

要开始使用node.js,我建议您查看O'Reilly的Up and running with node.js。模块化系统也在那里解释。

为了将MongoDB与节点一起使用,您必须使用通过npm安装的软件包(如mongoose或mongolian)。两者都在我上面提到的书中解释。

1

眼看方式你已经使用过它,似乎你正在尝试使用Require.js

Node.js不是客户端代码。你不需要Require.js,已经有require可用。

用例:

// Loads the mongodb-client module in the "mongo" variable 
var mongo = require('mongodb-client'); 

而且,没有document可用,你不是在浏览器中,没有任何DOM。如果你想要一个,你可以使用jsdom,但我不认为这是你想要的。

总结一下:不要以为你在浏览器中。你不是。没有document,没有window,不需要自定义加载,你可以在服务器环境下编程。

我强烈建议您阅读Node Beginner

The aim of this document is to get you started with developing applications with Node.js, teaching you everything you need to know about "advanced" JavaScript along the way. It goes way beyond your typical "Hello World" tutorial.