As soon as I am connecting my database with express using
const db = require("./config/mongoose");
It shows error
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...
How can I get out of it?
This type of error generally comes when you do not provide valid JSON data.
For ex:-
JSON.stringify({x:})
return error like this
Uncaught SyntaxError: Unexpected token '}'
Please check your provided JSON data again.
And after that you still get error please show your mongoose connection code.
This code is working fine. I have not got any error. I got successful connection message in console Connected to Database:: MongoDB.
Try this code it will also work for you.
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;
Related
I am trying to use socket.io on one of a simple project but when I run the same on my local machine I am getting this bug. I am not able to figure out what is going wrong my index.js is as follows
const app = require("express")();
const http = require("http").Server(app);
const io = require("socket.io")(http);
const port = process.env.PORT || 8080;
app.get("/", function(req, res) {
res.render("index.ejs");
});
http.listen(port, function() {
console.log("Listening on *:" + port);
});
The full log is here
Desktop/chat/node_modules/ws/lib/websocket.js:347
...options
^^^
SyntaxError: Unexpected token ...
at createScript (vm.js:74:10)
at Object.runInThisContext (vm.js:116:10)
at Module._compile (module.js:533:28)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Module.require (module.js:513:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (Desktop/chat/node_modules/ws/index.js:3:19)
My node version is 8.0
This looks like it's probably the object spread operator. If you showed the actual line of code that causes the error, we could see for sure.
You will need at least node v8.6 to get support for the object spread operator by default. Starting with node v8.21, it could be enabled via command line flags.
Here is my error captured:
and here are my coding files:
server file
keys file
I just create connection to mlab using mongodb, node and reactjs. I'm using mongo db version 4.
need your help guys.
[nodemon] starting `node server.js`
Error: URL malformed, cannot be parsed
at module.exports (C:\MERNapps\TestMERN08\node_modules\mongodb\lib\url_parser.js:17:21)
at deprecated (internal/util.js:47:15)
at connect (C:\MERNapps\TestMERN08\node_modules\mongodb\lib\operations\mongo_client_ops.js:179:3)
at connectOp (C:\MERNapps\TestMERN08\node_modules\mongodb\lib\operations\mongo_client_ops.js:283:3)
at executeOperation (C:\MERNapps\TestMERN08\node_modules\mongodb\lib\utils.js:420:24)
at MongoClient.connect (C:\MERNapps\TestMERN08\node_modules\mongodb\lib\mongo_client.js:168:10)
at Promise (C:\MERNapps\TestMERN08\node_modules\mongoose\lib\connection.js:493:12)
at new Promise (<anonymous>)
at NativeConnection.Connection.openUri (C:\MERNapps\TestMERN08\node_modules\mongoose\lib\connection.js:490:19)
at Mongoose.connect (C:\MERNapps\TestMERN08\node_modules\mongoose\lib\index.js:230:15)
at Object.<anonymous> (C:\MERNapps\TestMERN08\server.js:12:2)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
(node:3696) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
server running on port 5000
The issue is with your keys. Try using:
module.exports= {
mongoURI : "your-uri-string",
options:{key:value}
}
And use the keys as:
mongoose.connect(db.mongoURI,options);
a very common error with MongoDB atlas. better still follow this procedure
Install Mongo DB compass
use Default setting
hostname: localhost & port 27017
create a database with the project name and then collection: user
Connect with the code below on entry js. route
mongoose.connect('mongodb://localhost/projectname')
.then(() => console.log('mongo db working'))
.catch(err => console.log('ERROR is', err))
I'm trying to make a connection to MongoDB using Mongoose. But it does neither throw a error nor connect to the database. Following is my code.
const express = require('express');
const app = express();
const port = process.env.PORT || 8080;
const mongoose = require('mongoose');
console.log('Hi, there!!');
mongoose.connect('mongodb://localhost:27017/db_name', (err) => {
console.log('Callback called');
if(err) throw err;
console.log('Connected to database');
})
In the above code none of the console.log inside the callback do happen. But any place outside the mongoose.connect do work like console.log('Hi, there!!')
Versions Used
express: 4.0.0
mongoose: 3.8.40
node: 7.7.3
mongodb: 3.4.0
Using mongoose: 3.8.40 I got this in the console :
{ Error: Cannot find module '../build/Release/bson'
at Function.Module._resolveFilename (module.js:470:15)
at Function.Module._load (module.js:418:25)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/kevin/nemeacreation/sites/test/stackoverflow/node_modules/bson/ext/index.js:15:10)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3) code: 'MODULE_NOT_FOUND' }
js-bson: Failed to load c++ bson extension, using pure JS version
Hi, there!!
upgrading to "mongoose": "~4.4" fixed it for me. I got the answer here : https://stackoverflow.com/a/35516644/2829540
For info the latest release of mongoose is 4.10.4
'use strict';
var MongoClient;
MongoClient = require('mongodb').MongoClient();
MongoClient.connect(
'mongodb://127.0.0.1:27017/accounting',
function (err, connection){
var collection = connection.collection('customers');
collection.insert({'name': 'John Doe'}, function(err, count){
collection.find().toArray(function(err, documents){
console.dir(documents);
connection.close();
});
});
});
Getting this error when using this code, I would like to know what is causing the error and any possible fixes.
TypeError: Cannot read property 'connect' of undefined
at Object.<anonymous> (C:\Users\Matt\WebstormProjects\keyword-wrangler\index.js:6:12)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3
Not entirely sure what is causing this, I am using WebStorm and have installed the latest version of mongodb.
Correct way
var MongoClient = require('mongodb').MongoClient; // it's not a function
Your way
var MongoClient = require('mongodb').MongoClient();
Docs
I'm new in using full stack to build an application, i tried to create a connection that allow to the client side to connect to my mongo database. here my server.js code file
console.log("Server running...!");
var mongo=require('mongodb').MongoClien,
client=require('socket.io').listen(8080).sockets;
mongo.connect('localhost:27017/chat',function(err,db){
if(err) throw err;
client.on('connection',function(socket){
console.log('someone has connected !');
//waiting for input
socket.on('input',function(data){
console.log(data);
});
});
});
when i run this with node.js i get the following error in my command prompt window.
Server running...!
C:\Users\azus\Desktop\Psirt\code-master\server.js:6
mongo.connect('mongodb://127.0.0.1:27017/chat',function(err,db){
^
TypeError: Cannot read property 'connect' of undefined
at Object.<anonymous> (C:\Users\azus\Desktop\Psirt\code-master\server.js:6:7
)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:139:18)
at node.js:999:3
I checked my mongo server and it's running !!
I am working with the latest up to date version, i'm doing this on windows.
'MongoClien' is unknown
You are trying to end a line with a comma
Change
var mongo=require('mongodb').MongoClien,
to:
var mongo = require('mongodb').MongoClient;