GVKun编程网logo

'process.nextTick(function(){throw err;})'-未定义不是函数(mongodb / mongoose)(module未定义)

10

在本文中,您将会了解到关于'process.nextTick的新资讯,同时我们还将为您解释function的相关在本文中,我们将带你探索'process.nextTick的奥秘,分析function的

在本文中,您将会了解到关于'process.nextTick的新资讯,同时我们还将为您解释function的相关在本文中,我们将带你探索'process.nextTick的奥秘,分析function的特点,并给出一些关于$(document).ready(function()VS $(function(){、ArgumentException:MongoDB + Unity2D 中的无效关键字 'mongodb+srv://test:test@HOST' - Live MongoDB 未连接、Azure Cosmos DB - 间歇性 MongoConnectionException / IOException / SocketException、com.mongodb.MongoServerException的实例源码的实用技巧。

本文目录一览:

'process.nextTick(function(){throw err;})'-未定义不是函数(mongodb / mongoose)(module未定义)

'process.nextTick(function(){throw err;})'-未定义不是函数(mongodb / mongoose)(module未定义)

我正在尝试使用nodejs和socket.io 连接到我的 mongodb 。我能够连接到数据库,因为一旦我-确实-
获得了我在控制台中但在Node.js端上的“连接接受”

通过猫鼬建立与mongodb:// localhost:27017的连接

接下来它立即失败

process.nextTick(function(){throw err;})^
TypeError:undefined不是showCollections中的函数**

这里是showCollections:

var showCollections = function(db, callback) {     mongoose.connection.db.collectionNames(function(error, names) {    if (error) {      throw new Error(error);    } else {        console.log("=>Listening mongo collections:");      names.map(function(cname) {        mongoose.connection.db.dropCollection(cname.name);        console.log("--»"+cname.name);      });    }  });}

这是我的数据库文件夹的内容:

_tmp (empty folder)local.0local.nsmongod.lock

我通过键入 mongod –dbpath文件夹 运行mongodb ,它成功地“ 唤醒 了端口27017上的连接”。

另外,我来自 package.json (npm)的node_modules

"dependencies": {    "express": "^4.9.6",    "socket.io": "latest",    "mongodb": "~2.0",    "mongoose": "*"  }

非常感谢您的帮助…

堆栈跟踪:

> TypeError: undefined is not a function>     at showCollections (/usr/share/nginx/www/index.js:77:25)>     at NativeConnection.callback (/usr/share/nginx/www/index.js:46:3)>     at NativeConnection.g (events.js:199:16)>     at NativeConnection.emit (events.js:104:17)>     at open (/usr/share/nginx/www/node_modules/mongoose/lib/connection.js:485:10)>     at NativeConnection.Connection.onOpen (/usr/share/nginx/www/node_modules/mongoose/lib/connection.js:494:5)>     at /usr/share/nginx/www/node_modules/mongoose/lib/connection.js:453:10>     at /usr/share/nginx/www/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:59:5>     at /usr/share/nginx/www/node_modules/mongoose/node_modules/mongodb/lib/db.js:200:5>     at connectHandler (/usr/share/nginx/www/node_modules/mongoose/node_modules/mongodb/lib/server.js:272:7)

编辑:

当尝试运行nodejs实例时,我也会遇到这些问题:

{ [Error: Cannot find module ''../build/Release/bson''] code: ''MODULE_NOT_FOUND'' }js-bson: Failed to load c++ bson extension, using pure JS version

我尝试修复它们,因为这里的其他问题会告诉我,但是也没有任何效果…

答案1

小编典典

根据提供的信息,您似乎正在使用mongodb 2.0驱动程序。db.collectionNames方法已删除。查看此页面的“ Db对象”部分-https:
//github.com/mongodb/node-mongodb-
native/blob/0642f18fd85037522acf2e7560148a8bc5429a8a/docs/content/tutorials/changes-
from-1.0.md#L38

他们已将其替换为listCollections。您应该获得与以下相同的效果:

mongoose.connection.db.listCollections().toArray(function(err, names) {    if (err) {        console.log(err);    }    else {        names.forEach(function(e,i,a) {            mongoose.connection.db.dropCollection(e.name);            console.log("--->>", e.name);        });    }});

$(document).ready(function()VS $(function(){

$(document).ready(function()VS $(function(){

$(document).ready(function(){})vs $(function(){})
和我应该以$表格或新jQuery(document).ready(function(){ })方式书写的区别是什么?

如果我加载google.setOnLoadCallback(function() {了Google api是更好的方法?还是同一件事?

我也看到人们使用 $(function($){})

有人可以帮我,我迷路了。当我不知道所写的代码时,我会烦扰我。我想我应该阅读一下图书馆。全部都定义为同一件事吗?

答案1

小编典典

两种方式是等效的,我个人更喜欢第二种方式,$(function(){});这只是准备文档的捷径。

关于new jQuery(document)...构造,您实际上不需要使用new运算符,如果不需要,jQuery会在内部使用它。

ready处理程序函数接收的参数是jQuery对象本身。

这在必须与其他库以兼容模式运行jQuery时非常有用,例如:

jQuery(function ($) {  // use $ here});

$回调内部参数将引用jQuery对象,之外的功能,它可能是指像PrototypeJS另一个库。

ArgumentException:MongoDB + Unity2D 中的无效关键字 'mongodb+srv://test:test@HOST' - Live MongoDB 未连接

ArgumentException:MongoDB + Unity2D 中的无效关键字 'mongodb+srv://test:test@HOST' - Live MongoDB 未连接

如何解决ArgumentException:MongoDB + Unity2D 中的无效关键字 ''mongodb+srv://test:test@HOST'' - Live MongoDB 未连接?

我试图在本地连接我的 Live MongoDB 数据库,但在我使用外部字符串而不是 localhost 时出错

我使用:

  • MongoDB 数据库 v4.0.x(使用 mongoDB 的免费集群)
  • Unity 2020.3.13

如果我使用 localhost,即使所有的插入、更新、删除查询都能正常工作,这段代码也能正常工作

using UnityEngine;
using UnityEngine.UI;
using MongoDB.Bson;
using MongoDB.Driver;
using System.Collections;
using UnityEngine.Networking;
using MongoDB.Driver.Builders;
using UnityEngine.SceneManagement;
using System;
using Newtonsoft.Json ;

public class Login_Screen : MonoBehavIoUr {

public InputField usernameedit;
public InputField passwordone;
public Button loginButton;


  void start(){
    string connectionString = "mongodb://localhost:27017";
    var client = new MongoClient(connectionString);
    var server = client.GetServer();
    var database = server.GetDatabase("admin");
    var playercollection = database.GetCollection<BsonDocument>("users");
  }
}

但是我用于 LIVE 数据库调用的相同内容,然后导致以下错误。甚至连不上。

string connectionString = "mongodb+srv://testUsername:testPassword@testClustor.mongodb.net?retryWrites=true&w=majority";

错误信息如下:

ArgumentException: Invalid keyword ''mongodb+srv://testusername:testpassword@xyz.xxx.mongodb.net/admin1?w''. MongoDB.Driver.MongoConnectionStringBuilder.set_Item (System.String keyword,System.Object value) (at <6da29fc855c44d33ad78b3e27475ff27>:0) System.Data.Common.DbConnectionStringBuilder.set_ConnectionString (System.String value) (at <290425a50ff84a639f8c060e2d4530f6>:0) MongoDB.Driver.MongoConnectionStringBuilder..ctor (System.String connectionString) (at <6da29fc855c44d33ad78b3e27475ff27>:0) MongoDB.Driver.MongoClient.ParseConnectionString (System.String connectionString) (at <6da29fc855c44d33ad78b3e27475ff27>:0) MongoDB.Driver.MongoClient..ctor (System.String connectionString) (at <6da29fc855c44d33ad78b3e27475ff27>:0) Login_Screen.DoLogin () (at Assets/Script/Login_Screen.cs:61) UnityEngine.Events.InvokableCall.Invoke () (at <24599fe2776145d58ab771516c063d56>:0) UnityEngine.Events.UnityEvent.Invoke () (at <24599fe2776145d58ab771516c063d56>:0) UnityEngine.UI.Button.Press () (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:68) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:110) UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler,UnityEngine.EventSystems.BaseEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:50) UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target,UnityEngine.EventSystems.BaseEventData eventData,UnityEngine.EventSystems.ExecuteEvents+EventFunction1[T1] functor) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:262) UnityEngine.EventSystems.EventSystem:Update() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:385)

enter image description here

我真的浪费了 2 天的时间来寻找解决方案,请任何人帮助我。提前致谢

解决方法

我遇到了同样的问题。检查您的 db 和 db 用户的设置 - 确保它们可以从外部 IP 访问。

我像这样修改了我的连接字符串:

 private readonly Dictionary<string,string> _config = new Dictionary<string,string>()
        {
            {"dbUser","xxxxxx"},{"dbName","YourDbName"},{"password","xxxxxxxxxxxxx"}
        };

        public IMongoDatabase CreateInstance()
        {
            var client = new MongoClient($"mongodb+srv://{_config["dbUser"]}:{HttpUtility.UrlEncode(_config["password"])}@pocdata.0u29g.mongodb.net/{HttpUtility.UrlEncode(_config["dbName"])}?retryWrites=true&w=majority");
            return client.GetDatabase(_config["dbName"]);
        }

我只是使用公共方法连接到数据库,但这不是强制性的 - 您可以直接替换值 - 只需对密码和数据库名称进行 url 编码

Azure Cosmos DB - 间歇性 MongoConnectionException / IOException / SocketException

Azure Cosmos DB - 间歇性 MongoConnectionException / IOException / SocketException

如何解决Azure Cosmos DB - 间歇性 MongoConnectionException / IOException / SocketException?

我将 Azure Cosmos DB 4.0 与 MongoDB C# 驱动程序 2.10.4 结合使用。

大多数时候查询工作正常,但我遇到了这样的间歇性错误:

MongoDB.Driver.MongoConnectionException:向服务器发送消息时发生异常。 System.IO.IOException:无法将数据写入传输连接:远程主机强行关闭了现有连接。 System.Net.sockets.socketException: 一个现有的连接被远程主机强行关闭 在 System.Net.sockets.socket.BeginSend(... 在 System.Net.sockets.NetworkStream.BeginWrite --- 内部异常堆栈跟踪结束 --- 在 System.Net.sockets.NetworkStream.BeginWrite 在 System.Net.Security._SslStream.StartWriting 在 System.Net.Security._SslStream.ProcessWrite 在 System.Net.Security._SslStream.BeginWrite

发生该错误时,调用需要 10-25 秒才能失败。

我正在使用 @H_301_9@new MongoClient(MongoClientSettings.FromConnectionString(cnstr)) 构建 MongoClient,并且我使用的是带有这些参数 @H_301_9@?ssl=true&replicaset=globaldb&retrywrites=false 的连接字符串。

我尝试使用 @H_301_9@retryWrites=true(根据 Azure 支持建议),但这没有帮助。

我尝试了不同的设置,但都不起作用(@H_301_9@connect=direct、@H_301_9@maxIdleTimeMS=30000、@H_301_9@serverSelectionTimeout=5000ms、@H_301_9@socketTimeout=10000ms)。

导致这些异常的原因是什么?

解决方法

修复是设置/强制使用 TLS 1.2(基于 this Microsoft document):

//return new MongoClient(connectionString);
var settings = MongoClientSettings.FromConnectionString(connectionString);
settings.SslSettings = new SslSettings()
{
    EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls12
};
return new MongoClient(settings);

看起来虽然我的连接字符串有 ssl=true,但在某些服务器上还不够(错误是间歇性的)。 forcing TLS 1.2 通常可以修复相同的潜在错误,因此我认为在 Mongo 中它可能是相同的问题 - 它确实解决了问题。

com.mongodb.MongoServerException的实例源码

com.mongodb.MongoServerException的实例源码

项目:sam    文件:MongoServerExceptionMapper.java   
@Override
public Response toResponse(MongoServerException exc) {

  logger.error("Got mongo server exception",exc);
  final Status errorCode = Status.INTERNAL_SERVER_ERROR;
  return Response.status(errorCode)
    .entity(new ErrorMessage(
      errorCode.getStatusCode(),"Database exception: " + exc.getCode(),exc.getLocalizedMessage()
  )).build();
}

我们今天的关于'process.nextTickfunction的分享已经告一段落,感谢您的关注,如果您想了解更多关于$(document).ready(function()VS $(function(){、ArgumentException:MongoDB + Unity2D 中的无效关键字 'mongodb+srv://test:test@HOST' - Live MongoDB 未连接、Azure Cosmos DB - 间歇性 MongoConnectionException / IOException / SocketException、com.mongodb.MongoServerException的实例源码的相关信息,请在本站查询。

本文标签: