错误:未找到连接策略MongoDB

时间:2016-01-16 01:02:59

标签: mongodb express-session connect-mongo

这里是一个使用快速会话存储的简单连接,即使文本是正确的,它仍然会发出这个错误。我很确定这与“新的MongoStore”对象初始化有关。

var express = require('express'),
    expressSession = require('express-session');

var MongoStore = require('connect-mongo/es5')(expressSession);
var sessionStore = new MongoStore({
  host: '127.0.0.1',
  port: '27017',
  db: 'session'
});

var app = express()
    .use(expressSession({
      secret: 'my secret sign key',
      store: sessionStore
     }))
    .use('/home', function (req, res) {
      if (req.session.views) {
        req.session.views++;
      }
      else {
        req.session.views = 1;
      }
      res.end('Total views for you:' + req.session.views);
    })
    .use('/reset', function(req, res) {
      delete req.session.views;
      res.end('Cleared all your views');
    })
    .listen(3000);

4 个答案:

答案 0 :(得分:9)

将网址添加到新 var sessionStore = new MongoStore({ host: '127.0.0.1', port: '27017', db: 'session', url: 'mongodb://localhost:27017/demo' });

    class GetDealsByNameDescAdd extends AsyncTask<Void, Void, ArrayList<ListItems>> { 
     private final String URL_STRING = url;
    HttpClient httpclient = new DefaultHttpClient();
     HttpGet httpget = new HttpGet(URL_STRING);
    ArrayList<ListItems> itemsArray;
    String jsonData = null;

    @Override 
     protected ArrayList<ListItems> doInBackground(Void... params){
     try {
                HttpResponse response = httpclient.execute(httpget);
                jsonData =  EntityUtils.toString(response.getEntity());
                Log.d(TAG, String.format("JSON:", jsonData));

            } catch (IOException e) {
                e.printStackTrace();
            }

    itemsArray = new ArrayList<ListItems>();
            JSONObject object = null;
            try {
                object = new JSONObject(jsonData);
                JSONArray jArray  = object.getJSONArray("DATA");

                for(int i = 0; i < jArray.length(); i++){
                    JSONObject jObj = jArray.getJSONObject(i);

                    ListItems item=new ListItems();

                    //String Id = jObj.getString("id"); //replce this with following code
                    item.setId(jObj.getString("id"));

                    //String Name = jObj.getString("name");  //replce this with following code
                    item.setName(jObj.getString("name"));

                    //String Description = jObj.getString("description");   //replce this with following code
                    item.setDescription(jObj.getString("description"));

                    itemsArray.add(items);

                }
            } catch (JSONException e) {
                e.printStackTrace();
                //if there is an exception in parsing json then it returns null
                return null;
            }


            return itemsArray;
    }
    public void onPostExecute(ArrayList<ListItems> items){
        if(items!=null){
            //here you will receive valid set of data 
            //and you can add it to your adapter
        }else{
            //this part will execute, If we are having JSON exception
            // so we need to check the JSON response 
            //and here we should handle this NPE

            //show toast that we are receiving BAD JSON response
        }
    }
}

问题中的代码来自Basarat Ali Syed 的 Beginning Node.js 一书。

答案 1 :(得分:5)

因为你正在使用会话集合所以它应该是这样的

// Remove the active class from every element that starts with "level"
$('[id^="level"]').removeClass('activeLicenseLevel');
$('#level' + applicationLicenseLevel).addClass('activeLicenseLevel');

答案 2 :(得分:1)

查看http://sailsjs.com/documentation/reference/configuration/sails-config-session使用其他可连接连接的会话商店部分。

connect-mongo 模块存在版本限制,示例显示的参数集与文档中的参数不同:

  // Note: user, pass and port are optional
  adapter: 'connect-mongo',
  url: 'mongodb://user:pass@host:port/database',
  collection: 'sessions',
  auto_reconnect: false,
  ssl: false,
  stringify: true

我实际上只需要使用:

  // Note: user, pass and port are optional
  adapter: 'connect-mongo',
  url: 'mongodb://localhost:27017/sails',
  collection: 'sessions'

希望它有所帮助!

答案 3 :(得分:0)

您需要定义数据库连接,然后将其传递给新的MongoStore。 &#39; db:&#39;您正在使用的参数是期望MongoDB驱动程序连接,而不是Mongo数据库的URL。为此,你应该这样做:

var sessionStore = new MongoStore({ url:'mongodb://localhost:27017/test');

这是我知道的一个例子,虽然它使用的是Mongoose而不是MongoDB驱动程序。

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test');
var sessionStore = new MongoStore({mongooseConnection: mongoose.connection });