Node.js mongodb 设置默认安全变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12846238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 15:57:23  来源:igfitidea点击:

Node.js mongodb set default safe variable

node.jsmongodb

提问by Farhan Ahmad

I am trying to run a Node.js script locally and it's giving me this error message:

我正在尝试在本地运行 Node.js 脚本,但它给了我以下错误消息:

========================================================================================
=  Please ensure that you set the default safe variable to one of the                  =
=   allowed 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 false will change to true in the near future                         =
=                                                                                      =
=  This message will disappear when the default safe is set on the driver Db           =
========================================================================================
========================================================================================
=  Please ensure that you set the default safe variable to one of the                  =
=   allowed 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 false will change to true in the near future                         =
=                                                                                      =
=  This message will disappear when the default safe is set on the driver Db           =
========================================================================================

Here are my variables:

这是我的变量:

var express = require('express');

var mongodb = require('mongodb');
var GridStore = require('mongodb').GridStore;
var Db = require('mongodb').Db;
var Server = require('mongodb').Server;
var db = new Db(Config.dbName, new Server("127.0.0.1", 27017, {}), {});    

var HttpGet = require('./httpGet').HttpGet;
var URL = require('url');

var dbClient = null; // this is initialized when db is opened
var app = module.exports = express();

The same scripts runs fine on my live server. It only complanes when I run it locally.

相同的脚本在我的实时服务器上运行良好。只有当我在本地运行它时它才会抱怨。

I found this same issue being discussed on github but found no solution. https://github.com/kissjs/node-mongoskin/issues/77

我发现在 github 上讨论了同样的问题,但没有找到解决方案。 https://github.com/kissjs/node-mongoskin/issues/77

Anyone know what could cause this problem?

有谁知道是什么导致了这个问题?

Thanks in advance :)

提前致谢 :)

回答by JohnnyHK

The following works for me using the 1.1.11 mongo driver:

以下内容适用于使用 1.1.11 mongo 驱动程序的我:

var db = new Db(Config.dbName, new Server("127.0.0.1", 27017, {}), {safe: true});

Without the {safe: true}parameter I do get the same warning as you show in your question.

如果没有{safe: true}参数,我会收到与您在问题中显示的警告相同的警告。

This warning was a very recent addition to the driver; you're probably using an older version of the driver on your server which is why you don't see the warning there.

这个警告是驱动程序最近添加的;您的服务器上可能使用的是旧版本的驱动程序,这就是为什么您在那里看不到警告的原因。

回答by Farhan Ahmad

I got it to work by setting the strictmode to false.

我通过将strict模式设置为 false使其工作。

var db = new Db(config.dbName, new Server("127.0.0.1", 27017, {}), {safe: false, strict: false}); 

回答by KiranYNG

This worked for me!

这对我有用!

var db = new Db((new DbServer('127.0.0.1', 27017), {w:-2,journal:false,fsync:false,safe: false})