【问题标题】:Trouble deploying to Heroku using Mongoskin and MongoHQ with Nodejs使用带有 Nodejs 的 Mongoskin 和 MongoHQ 部署到 Heroku 时遇到问题
【发布时间】:2013-09-12 17:38:19
【问题描述】:

我目前正在构建一个使用 mongoskin 的 node/express.js 应用程序。我最近将我的应用程序部署到 Heroku,并为将 mongohq 与我的应用程序结合使用而头疼。

这是我使用 node app.js 运行的 app.js 文件

var express = require("express");
var app = express();
decks = require('./routes/decks');
app.get('/decks', decks.findAll);

我的 package.json:

{
    "name": "blah blah",
    "description": "Why are there so my blah blah",
    "version": "0.0.1",
    "private": true,
    "dependencies": {
        "express": "3.x",
        "mongodb": "1.1.8",
        "socket.io": "0.9.10"
    },
    "engines": {
        "node": "0.8.4",
        "npm": "1.1.49"
    }
}

在阅读了 Heroku 上的指南后,我尝试重构 decks.js 并像这样使用 mongoskin。

var mongo = require('mongoskin'); 
var mongoUri = process.env.MONGOHQ_URL;
var db = mongo.db(mongoUri);

exports.findById = function(req, res) {
     var id = req.params.id;
     console.log('Retrieving deck: ' + id);
     db.collection('decks', function(err, collection) {
         collection.findOne({'_id':new BSON.ObjectID(id)}, function(err, item) {
             res.send(item);
         });
     });
 };

但是我一直收到错误:

 Please ensure that you set the default write concern for the database by setting    =
=   one of the options                                                                 =
=                                                                                      =
=     w: (value of > -1 or the string 'majority'), where < 1 means                     =
=        no write acknowlegement                                                       =
=     journal: true/false, wait for flush to journal before acknowlegement             =
=     fsync: true/false, wait for flush to file system before acknowlegement           =
=                                                                                      =
=  For backward compatibility safe is still supported and                              =
=   allows values of [true | false | {j:true} | {w:n, wtimeout:n} | {fsync:true}]      =
=   the default value is false which means the driver receives does not                =
=   return the information of the success/error of the insert/update/remove            =
=                                                                                      =
=   ex: new Db(new Server('localhost', 27017), {safe:false})                           =
=                                                                                      =
=   http://www.mongodb.org/display/DOCS/getLastError+Command                           =
=                                                                                      =
=  The default of no acknowlegement will change in the very near future                =
=                                                                                      =
=  This message will disappear when the default safe is set on the driver Db           =
========================================================================================

我已经阅读了大量教程,但我无法让我的数据库正常工作!!如有任何帮助,我们将不胜感激。

【问题讨论】:

    标签: node.js mongodb heroku mongohq mongoskin


    【解决方案1】:

    您的 Db 对象正在使用不推荐使用的设置:“安全”。

    如果您使用所需的写入问题设置“w”选项,则该错误应该会消失。

    这是用于实例化 Db 对象的文档的链接。

    http://mongodb.github.io/node-mongodb-native/api-generated/db.html

    ...

    哦,您可以尝试将 uri 变量更新为 process.env.MONGOHQ_URL :P

    【讨论】:

      【解决方案2】:

      supershabam 说的是对的

      修改这一行:

      var db = mongo.db(mongoUri);
      

      到这里:

      var db = mongo.db(mongoUri, {w:1});
      

      这将在对数据库执行操作时给您写确认,并使错误消失

      有关写入问题的更多信息,请查看link out

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-02-18
        • 2021-01-20
        • 2021-11-08
        • 2018-10-31
        • 2012-05-19
        • 2016-08-27
        • 1970-01-01
        相关资源
        最近更新 更多