我目前正在构建一个使用mongoskin的node / express.js应用。我最近将我的应用程序部署到了Heroku,并且使用mongohq来处理我的应用程序时遇到了很多麻烦。
这是我用节点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 =
========================================================================================
我已经阅读了大量的教程,但我不能让我的数据库工作!如有任何帮助,请您表示赞赏。
答案 0 :(得分:1)
您的Db对象使用的是已弃用的设置:“safe”。
如果使用所需的写入关注点设置“w”选项,则该错误应该消失。
这是指向实例化Db对象的文档的链接。
http://mongodb.github.io/node-mongodb-native/api-generated/db.html
...
哦,你可以尝试将你的uri变量更新为process.env.MONGOHQ_URL:P
答案 1 :(得分:0)
什么是supershabam所说的是对的
修改此行:
var db = mongo.db(mongoUri);
到此:
var db = mongo.db(mongoUri, {w:1});
这将使您在对数据库执行操作时写入确认,并使错误消失
有关写入问题的详细信息,请查看此link