2012-06-13 46 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.