我的程序的'npm start'命令没有与Mongod的本地主机连接,这看起来工作错误



我正在开发一个注册帐户的原型程序,我正在使用Node.JS在JavaScript中编程,以便我可以将其应用于移动设备。

我得到MongoDB下载并创建了数据/db目录,以适应它,因为我使用它的4.0.2版本让我能够在Windows 8.1上使用它。我尝试在命令提示符中使用mongod命令使本地服务器与npm一起启动,但据我所知,mongo不能正常工作。当我尝试运行它时,它打印出以下内容:

C:Usersuser>mongod
2021-08-03T17:40:37.119-0300 I  CONTROL  [main] Automatically disabling TLS 1.0,
to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2021-08-03T17:40:38.707-0300 W  ASIO     [main] No TransportLayer configured dur
ing NetworkInterface startup
2021-08-03T17:40:38.710-0300 I  CONTROL  [initandlisten] MongoDB starting : pid=
3624 port=27017 dbpath=C:datadb 64-bit host=user-pc
2021-08-03T17:40:38.710-0300 I  CONTROL  [initandlisten] targetMinOS: Windows 7/
Windows Server 2008 R2
2021-08-03T17:40:38.711-0300 I  CONTROL  [initandlisten] db version v4.2.15
2021-08-03T17:40:38.711-0300 I  CONTROL  [initandlisten] git version: d7fd78dead
621a539c20791a93abec34bb1be385
2021-08-03T17:40:38.711-0300 I  CONTROL  [initandlisten] allocator: tcmalloc
2021-08-03T17:40:38.711-0300 I  CONTROL  [initandlisten] modules: none
2021-08-03T17:40:38.711-0300 I  CONTROL  [initandlisten] build environment:
2021-08-03T17:40:38.712-0300 I  CONTROL  [initandlisten]     distmod: 2012plus
2021-08-03T17:40:38.712-0300 I  CONTROL  [initandlisten]     distarch: x86_64
2021-08-03T17:40:38.712-0300 I  CONTROL  [initandlisten]     target_arch: x86_64
2021-08-03T17:40:38.712-0300 I  CONTROL  [initandlisten] options: {}
2021-08-03T17:40:38.716-0300 W  STORAGE  [initandlisten] Detected unclean shutdo
wn - C:datadbmongod.lock is not empty.
2021-08-03T17:40:38.719-0300 I  STORAGE  [initandlisten] Detected data files in
C:datadb created by the 'wiredTiger' storage engine, so setting the active st
orage engine to 'wiredTiger'.
2021-08-03T17:40:38.721-0300 W  STORAGE  [initandlisten] Recovering data from th
e last clean checkpoint.
2021-08-03T17:40:38.722-0300 I  STORAGE  [initandlisten] wiredtiger_open config:
create,cache_size=1087M,cache_overflow=(file_max=0M),session_max=33000,eviction
=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=
true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=
100000,close_scan_interval=10,close_handle_minimum=250),statistics_log=(wait=0),
verbose=[recovery_progress,checkpoint_progress],
2021-08-03T17:40:39.026-0300 I  STORAGE  [initandlisten] WiredTiger message [162
8023239:25693][3624:140728932634752], txn-recover: Recovering log 18 through 19
2021-08-03T17:40:39.377-0300 I  STORAGE  [initandlisten] WiredTiger message [162
8023239:377270][3624:140728932634752], txn-recover: Recovering log 19 through 19
2021-08-03T17:40:39.819-0300 I  STORAGE  [initandlisten] WiredTiger message [162
8023239:819672][3624:140728932634752], txn-recover: Main recovery loop: starting
at 18/256 to 19/256
2021-08-03T17:40:39.827-0300 I  STORAGE  [initandlisten] WiredTiger message [162
8023239:826508][3624:140728932634752], txn-recover: Recovering log 18 through 19
2021-08-03T17:40:40.324-0300 I  STORAGE  [initandlisten] WiredTiger message [162
8023240:324574][3624:140728932634752], file:index-3--5844570482911569703.wt, txn
-recover: Recovering log 19 through 19
2021-08-03T17:40:40.644-0300 I  STORAGE  [initandlisten] WiredTiger message [162
8023240:643927][3624:140728932634752], file:index-3--5844570482911569703.wt, txn
-recover: Set global recovery timestamp: (0, 0)
2021-08-03T17:40:41.297-0300 I  RECOVERY [initandlisten] WiredTiger recoveryTime
stamp. Ts: Timestamp(0, 0)
2021-08-03T17:40:41.308-0300 I  STORAGE  [initandlisten] No table logging settin
gs modifications are required for existing WiredTiger tables. Logging enabled? 1
2021-08-03T17:40:41.316-0300 I  STORAGE  [initandlisten] Timestamp monitor start
ing
2021-08-03T17:40:41.411-0300 I  CONTROL  [initandlisten]
2021-08-03T17:40:41.413-0300 I  CONTROL  [initandlisten] ** WARNING: Access cont
rol is not enabled for the database.
2021-08-03T17:40:41.417-0300 I  CONTROL  [initandlisten] **          Read and wr
ite access to data and configuration is unrestricted.
2021-08-03T17:40:41.418-0300 I  CONTROL  [initandlisten]
2021-08-03T17:40:41.419-0300 I  CONTROL  [initandlisten] ** WARNING: This server
is bound to localhost.
2021-08-03T17:40:41.420-0300 I  CONTROL  [initandlisten] **          Remote syst
ems will be unable to connect to this server.
2021-08-03T17:40:41.422-0300 I  CONTROL  [initandlisten] **          Start the s
erver with --bind_ip <address> to specify which IP
2021-08-03T17:40:41.425-0300 I  CONTROL  [initandlisten] **          addresses i
t should serve responses from, or with --bind_ip_all to
2021-08-03T17:40:41.426-0300 I  CONTROL  [initandlisten] **          bind to all
interfaces. If this behavior is desired, start the
2021-08-03T17:40:41.428-0300 I  CONTROL  [initandlisten] **          server with
--bind_ip 127.0.0.1 to disable this warning.
2021-08-03T17:40:41.433-0300 I  CONTROL  [initandlisten]
2021-08-03T17:40:41.441-0300 I  SHARDING [initandlisten] Marking collection loca
l.system.replset as collection version: <unsharded>
2021-08-03T17:40:41.452-0300 I  STORAGE  [initandlisten] Flow Control is enabled
on this deployment.
2021-08-03T17:40:41.454-0300 I  SHARDING [initandlisten] Marking collection admi
n.system.roles as collection version: <unsharded>
2021-08-03T17:40:41.457-0300 I  SHARDING [initandlisten] Marking collection admi
n.system.version as collection version: <unsharded>
2021-08-03T17:40:41.462-0300 I  SHARDING [initandlisten] Marking collection loca
l.startup_log as collection version: <unsharded>
2021-08-03T17:40:41.935-0300 W  FTDC     [initandlisten] Failed to initialize Pe
rformance Counters for FTDC: WindowsPdhError: PdhExpandCounterPathW failed with
'O objeto especificado não foi encontrado no computador.' for counter 'MemoryA
vailable Bytes'
2021-08-03T17:40:41.937-0300 I  FTDC     [initandlisten] Initializing full-time
diagnostic data capture with directory 'C:/data/db/diagnostic.data'
2021-08-03T17:40:41.946-0300 I  SHARDING [LogicalSessionCacheRefresh] Marking co
llection config.system.sessions as collection version: <unsharded>
2021-08-03T17:40:41.949-0300 I  SHARDING [LogicalSessionCacheReap] Marking colle
ction config.transactions as collection version: <unsharded>
2021-08-03T17:40:41.951-0300 I  NETWORK  [listener] Listening on 127.0.0.1
2021-08-03T17:40:41.952-0300 I  NETWORK  [listener] waiting for connections on p
ort 27017
2021-08-03T17:40:42.108-0300 I  FTDC     [ftdc] Unclean full-time diagnostic dat
a capture shutdown detected, found interim file, some metrics may have been lost
. OK
2021-08-03T17:40:42.126-0300 I  SHARDING [ftdc] Marking collection local.oplog.r
s as collection version: <unsharded>

我如何使它正常工作?

您的MongoDB实例工作正常,检查以下行:

2021-08-03T17:40:41.951-0300 I  NETWORK  [listener] Listening on 127.0.0.1
2021-08-03T17:40:41.952-0300 I  NETWORK  [listener] waiting for connections on port 27017

这意味着Mongo没事。另一方面,你的错误说:

UnhandledPromiseRejectionWarning: MongoError: Authentication failed.

那向你解释,你把你的凭据错误(用户名和密码),除此之外,你没有捕捉到错误,而试图连接到mongo(与try/catch)block)所以你得到了UnhandledPromiseRejectionWarning

npm开始是运行应用程序的默认npm脚本,它与MongoDB实例没有任何共同之处。

我的建议是下载Robo3T(或另一个客户端),并检查如果你可以连接到你的MongoDB实例从那里。完成后,可以继续从Node.js

测试连接希望它有助于澄清:)