简体   繁体   English

mongodb和node.js驱动程序未连接以聊天休息api

[英]mongodb and node.js drivers not connecting for chat rest api

I get errors when trying to connect to mongodb while using node.js/mongodb core modules. 使用node.js / mongodb核心模块尝试连接到mongodb时出现错误。

currently using node.js v0.10.25 and mongodb v2.6.1 当前使用的是node.js v0.10.25和mongodb v2.6.1

One of my errors is connecting to mongodb 我的错误之一是连接到mongodb

mongo.Db.connect(host, function(error, client) {                                                                                               
         ^                                                                                                                                     
TypeError: Object function (databaseName, topology, options) {

I also get this when running nodemon 运行nodemon时我也得到了

[nodemon] app crashed - waiting for file changes before starting...

here is my code for chat rest api 这是我的聊天休息api代码

// first we include our libraries
var http = require('http');
var util = require('util');
var querystring = require('querystring');
var mongo = require('mongodb');

// make a string to connect to MongoDB:
var host = "mongodb://localhost/test";

// We put all the logic inside of an open connection in the form of a callback function:
mongo.Db.connect(host, function(error, client) {
    console.log("this is working");
    if (error) throw error;

    var collection = new mongo.Collection(client, 'messages');

    var app = http.createServer( function (request, response) {

        if (request.method === "GET" && request.url === "/messages/list.json") {
            collection.find().toArray(function(error,results) {
                response.writeHead(200,{'Content-Type':'text/plain'});
                console.dir(results);
                response.end(JSON.stringify(results));
            });
        };

        if (request.method === "POST" && request.url === "/messages/create.json") {
            request.on('data', function(data) {
                collection.insert(querystring.parse(data.toString('utf-8')), {safe:true}, function(error, obj) {
                    if (error) throw error;
                    response.end(JSON.stringify(obj));
            })              
        })
    };
});

    var port = process.env.PORT || 3000;
    app.listen(port);
})

Hope someone can help me :) 希望可以有人帮帮我 :)

The problem was establishing a connection to mongodb driver. 问题是建立与mongodb驱动程序的连接。 The current method used was for mongodb v1.49 and has since been changed with the current version of mongodb v2.6.1. 当前使用的方法用于mongodb v1.49,此后已随mongodb v2.6.1的当前版本进行更改。

Here is the updated code to reflect the needed changes to connect to the current version of mongodb. 这是更新的代码,以反映连接到当前版本的mongodb所需的更改。

    /*
     * github https://github.com/azat-co/rpjs/tree/master/mongo
     *  
     * To test via CURL terminal commands and see list of messages run: 
     * $ curl http://localhost:3000/messages/list.json 
     * 
     * NOTE: Will return empty array
     * 
     * POST a new message:
     * $ curl -d "username=BOB&message=test" http://localhost:3000/messages/create.json 
     * 
     * To add new messages:
     * $ curl -X POST -d 'name=sosana&message=waz up' http://localhost:3000/messages/create.json 
     * 
     * To check if messages worked
     * $ curl localhost:3000/messages/list.json
     * 
    */

    // first we include our libraries
    var http = require('http');
    var util = require('util');
    var querystring = require('querystring');
    var MongoClient = require('mongodb').MongoClient;
    var mongo = new MongoClient();

    // make a string to connect to MongoDB:
    var host = "mongodb://localhost/test";

    // We put all the logic inside of an open connection in the form of a callback function:
    mongo.connect(host, function(error, db) {
        console.log("this is working");
        if (error) throw error;

        // establish a connection to the database
        var myDB = db.db("test");

        // establish a connection to the collection 
        var collection =  myDB.collection("messages");

        var app = http.createServer( function (request, response) {

            if (request.method === "GET" && request.url === "/messages/list.json") {
                collection.find().toArray(function(error,results) {
                    response.writeHead(200,{'Content-Type':'text/plain'});
                    console.dir(results);
                    response.end(JSON.stringify(results));
                });
            };

            if (request.method === "POST" && request.url === "/messages/create.json") {
                request.on('data', function(data) {
                    collection.insert(querystring.parse(data.toString('utf-8')), {safe:true}, function(error, obj) {
                        if (error) throw error;
                        response.end(JSON.stringify(obj));
                })              
            })
        };
});

    var port = process.env.PORT || 3000;
    app.listen(port);
    console.log("listening on port " + port);
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM