繁体   English   中英

将数据库(MongoDB)与快速服务器连接时出错

[英]Error in connecting Database(MongoDB) with express server

一旦我使用 express 连接我的数据库

const db = require("./config/mongoose");

它显示错误



SyntaxError: Invalid or unexpected token
    at wrapSafe (internal/modules/cjs/loader.js:979:16)
    at Module._compile (internal/modules/cjs/loader.js:1027:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (C:\Users\DELL\Documents\Vs Code Folders\Feelare\node_modules\whatwg-url\lib\url-state-machine.js:8:31)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
[nodemon] app crashed - waiting for file changes before starting...


我怎样才能摆脱它?

当您没有提供有效的 JSON 数据时,通常会出现此类错误。

例如:-

JSON.stringify({x:})

像这样返回错误

Uncaught SyntaxError: Unexpected token '}'

请再次检查您提供的JSON数据。

之后您仍然会收到错误消息,请出示您的mongoose连接代码。

这段代码工作正常。 我没有收到任何错误。 我在控制台中收到成功连接消息Connected to Database:: MongoDB

试试这个代码它也会为你工作。

const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/feelare_db");
const db = mongoose.connection;
db.on("error", console.error.bind(console, "Error in connecting to MongoDB "));
db.once("open", function () {
  console.log("Connected to Database:: MongoDB");
});

module.exports = db;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM