sei-ec-remote / project-2-issues

1 stars 0 forks source link

Issues: MongoStore error #132

Closed erisha closed 2 years ago

erisha commented 2 years ago

What's the problem you're trying to solve?

I'm getting an error message about my mongostore. Idk why. I tried changing the code around & reading documentation.

Post any code you think might be relevant (one fenced block per file)


require('dotenv').config()
const express = require('express')
const mongoose = require('mongoose')
const morgan = require('morgan')
const methodOverride = require('method-override')
const profileRoutes = require('./routes/profile_routes')
const userRoutes = require('./routes/user_routes')
const commentRoutes = require('./routes/comment_routes')
const blogPostRoutes = require('./routes/blogPost_routes')

const app = require('liquid-express-views')(express())

app.use(morgan('tiny'))
app.use(methodOverride('_method'))
app.use(express.urlencoded({ extended: false }))
app.use(express.static('public'))
const session = require('express-session')
const MongoStore = require( 'connect-mongo' ).default

//set up sessions
app.use(
    session({
        secret: process.env.SECRET,
        store: MongoStore.create({
            mongoUrl: process.env.DATABASE_URI
        }),
        saveUninitialized: true,
        resave: false
    })
)

app.use('/blogPost', blogPostRoutes)
app.use('/users', userRoutes)
app.use('/comments', commentRoutes)
app.use('/profile', profileRoutes)
// localhost:3000/

app.get('/', (req, res) => {
    //res.send('TechSpace Homepage')
    res.redirect('/techSpace')
})

const PORT = process.env.PORT

app.listen( PORT, ()=> {
    console.log(`Listening to TechSpace ${PORT}`)
})

If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?

~/sei_icepops/projects/Project2-TechSpace/TechSpace-project2 (main) » node server.js /Users/erisha/sei_icepops/projects/Project2-TechSpace/TechSpace-project2/server.js:40 store: MongoStore.create({ ^

TypeError: Cannot read properties of undefined (reading 'create') at Object. (/Users/erisha/sei_icepops/projects/Project2-TechSpace/TechSpace-project2/server.js:40:21) at Module._compile (node:internal/modules/cjs/loader:1105:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10) at Module.load (node:internal/modules/cjs/loader:981:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) at node:internal/main/run_main_module:17:47

What is your best guess as to the source of the problem?

Idk.

What things have you already tried to solve the problem?

Google, reading documentation & changing the code around. that didn't work I changed it back

Paste a link to your repository here

https://github.com/erisha/TechSpace-project2.git

timmshinbone commented 2 years ago

change this line:

const MongoStore = require( 'connect-mongo' ).default

to this:

const MongoStore = require( 'connect-mongo' )

and let me know if that clears up the error

erisha commented 2 years ago

no. It's not working. That is the code I started off with. this is the error message now:

node server.js Assertion failed: You must provide either mongoUrl|clientPromise|client in options /Users/erisha/sei_icepops/projects/Project2-TechSpace/TechSpace-project2/node_modules/connect-mongo/build/main/lib/MongoStore.js:119 throw new Error('Cannot init client. Please provide correct options'); ^

Error: Cannot init client. Please provide correct options at new MongoStore (/Users/erisha/sei_icepops/projects/Project2-TechSpace/TechSpace-project2/node_modules/connect-mongo/build/main/lib/MongoStore.js:119:19) at Function.create (/Users/erisha/sei_icepops/projects/Project2-TechSpace/TechSpace-project2/node_modules/connect-mongo/build/main/lib/MongoStore.js:136:16) at Object. (/Users/erisha/sei_icepops/projects/Project2-TechSpace/TechSpace-project2/server.js:40:21) at Module._compile (node:internal/modules/cjs/loader:1105:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10) at Module.load (node:internal/modules/cjs/loader:981:32)

erisha commented 2 years ago

PORT = 3000

DATABASE_URI:'mongodb://localhost/techSpace'

SECRET=iLoveKai

erisha commented 2 years ago

{ "name": "techspace-project2", "version": "1.0.0", "description": "TechSpace Networking App for Tech professionals", "main": "server.js", "scripts": { "test": "test", "start": "nodemon" }, "repository": { "type": "git", "url": "git+https://github.com/erisha/TechSpace-project2.git" }, "author": "Erisha Roach", "license": "ISC", "bugs": { "url": "https://github.com/erisha/TechSpace-project2/issues" }, "homepage": "https://github.com/erisha/TechSpace-project2#readme", "dependencies": { "bcryptjs": "^2.4.3", "connect-mongo": "^4.6.0", "dotenv": "^16.0.1", "express": "^4.18.1", "express-session": "^1.17.3", "liquid": "^5.1.1", "liquid-express-views": "^1.0.8", "method-override": "^3.0.0", "mongodb": "^4.8.0", "mongoose": "^6.4.4", "morgan": "^1.10.0" } }

erisha commented 2 years ago

connection.js

require('dotenv').config()

const mongoose = require('mongoose')

const DATABASE_URI = process.env.DATABASE_URI
const config = {
    useNewUrlParser: true,
    useUnifiedTopology: true
}

mongoose.connect(DATABASE_URI, config)

mongoose.connection

    .on('open', () => console.log('Connected to Mongoose'))

    .on('close', () => console.log('Disconnected from Mongoose'))

    .on('error', err => console.error(err))

module.exports = mongoose
timmshinbone commented 2 years ago

change this:

DATABASE_URI:'mongodb://localhost/techSpace'

to this

DATABASE_URI = 'mongodb://localhost/techSpace'