2013-12-10 201 views
0

我想在windows 8上使用node.js设置mongodb,有谁知道为什么即时通讯得到这个错误。 C:\ users \ phill \ node_modules \ mongodb \ lib \ mongodb \ mongo_client.js:359它也说在collection = db collection ,,,不能调用null的方法'collection'。我很难设置它。我的目标是能够添加到mongo数据库,并看到我添加或拉起我添加的内容,但现在添加一些对我来说已经足够好了。我尝试了所有可以找到的东西,即使是从网站上直接找到的,我也尝试了我在这里看到的所有内容。想想也许这是我设置事物的方式。我的node.js保存在我的c:驱动器中,有一个文件说,在那里我有node_modules,npm等程序文件(86x)。路径结束于:computer> windows(C :)>程序文件(86x)> nodejs。我的Mongodb保存在我的C:驱动器上,最终成为windows(C :)> mongodb-win32-x86_64-2008plus-2.4.8。在我的C:我也创建了一个文件数据,并在其中创建了另一个数据库。我被告知我应该只使用猫鼬,我只是在学习,所以我打开任何建议,链接或任何有助于。我还有最后一个问题,我学习了PHP,然后发现了关于sql注入和类似的东西,我根本没有看到任何有关安全的内容,我应该期待同样的事情。对于这个我得到的文字没有定义,但我已经得到错误,我已经完成了,最好的是我被困在一个正确的关注屏幕上。Node.js和mongodb访问mongodb

var MongoClient = require('mongodb').MongoClient; 

MongoClient.connect("mongodb://localhost:27017/integration_test", function(err, db) { 
test.equal(null, err); 
test.ok(db != null); 

db.collection("replicaset_mongo_client_collection").update({a:1},   
{b:1}, {upsert:true},   function(err, result) { 
test.equal(null, err); 
test.equal(1, result); 

db.close(); 
test.done(); 
}); 
}); 

尝试这样做以及和得到一个错误,C:\用户\菲尔\ node_modules \ mongodb的\ lib中\ mongodb的\ mongo_client.js:359 ....在收集=分贝收集,,,可以” t调用null的方法'collection'。即时通讯在命令提示符下调用它filename.js我将它保存在我的node.js文件所在的位置,之前我已经提取了文件并创建了一个服务器。

var Db = require('mongodb').Db, 
MongoClient = require('mongodb').MongoClient, 
Server = require('mongodb').Server, 
ReplSetServers = require('mongodb').ReplSetServers, 
ObjectID = require('mongodb').ObjectID, 
Binary = require('mongodb').Binary, 
GridStore = require('mongodb').GridStore, 
Grid = require('mongodb').Grid, 
Code = require('mongodb').Code, 
BSON = require('mongodb').pure().BSON, 
assert = require('assert'); 

var db = new Db('test', new Server('localhost', 27017)); 
// Fetch a collection to insert document into 
db.open(function(err, db) { 
var collection = db.collection("simple_document_insert_collection_no_safe"); 
// Insert a single document 
collection.insert({hello:'world_no_safe'}); 

// Wait for a second before finishing up, to ensure we have written the item to disk 
setTimeout(function() { 

// Fetch the document 
collection.findOne({hello:'world_no_safe'}, function(err, item) { 
assert.equal(null, err); 
assert.equal('world_no_safe', item.hello); 
db.close(); 
}) 
}, 100); 
}); 

回答

0

在你的第一个代码示例,你说:

为此,我得到的文本没有定义

我假设你的意思是 “测试没有定义?”您的脚本只需要mongodb库,我不相信test是一个核心nodejs函数,因此可以解释错误。

要引用db.collection()的驱动程序文档,使用了assert库,但也正确导入(如在第二个示例中那样)。

在您回拨至db.open()的范围内,您不检查是否发生错误。这可能会揭示为什么db在该函数中为null。

关于您关于MongoDB的SQL注入的等价问题,主要关注的领域是您可能将不受信任的输入传递到评估的JavaScript中,或使用此类输入来构造自由格式查询对象(而不是简单地使用字符串,但更像是将对象放入您的BSON查询中)。这两个链接都应该提供更多的信息,关于这个问题:

+0

感谢,种了这通快递的工作。还没有看安全性,但将被​​告阅读你的链接! TY! – user3081020