- #1
billllib
- 77
- 2
I am having an error setting up mern
https://www.digitalocean.com/community/tutorials/getting-started-with-the-mern-stack#testing-api
I have to assume I didn't follow the instruction properly.The error is under Database section from the link above. When I type
into the terminal. The error I get is
My directories look like this
Inside models is todo.js and inside routes is api.js.
Do you think inside .env I have the wrong password? Also in visual studio advance when I save .env it comes back as .env.txt
Here is what my files look like
todo.js
api.js
index.js
https://www.digitalocean.com/community/tutorials/getting-started-with-the-mern-stack#testing-api
I have to assume I didn't follow the instruction properly.The error is under Database section from the link above. When I type
Code:
require('dotenv').config()
into the terminal. The error I get is
Code:
Method invocation failed because [System.String] does not contain a method named 'config'.
At line:1 char:1
+ require('dotenv').config()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
My directories look like this
Inside models is todo.js and inside routes is api.js.
Do you think inside .env I have the wrong password? Also in visual studio advance when I save .env it comes back as .env.txt
Here is what my files look like
todo.js
JavaScript:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
//create schema for todo
const TodoSchema = new Schema({
action: {
type: String,
required: [true, 'The todo text field is required']
}
})
//create model for todo
const Todo = mongoose.model('todo', TodoSchema);
module.exports = Todo;
api.js
JavaScript:
const express = require ('express');
const router = express.Router();
const Todo = require('../models/todo');
router.get('/todos', (req, res, next) => {
//this will return all the data, exposing only the id and action field to the client
Todo.find({}, 'action')
.then(data => res.json(data))
.catch(next)
});
router.post('/todos', (req, res, next) => {
if(req.body.action){
Todo.create(req.body)
.then(data => res.json(data))
.catch(next)
}else {
res.json({
error: "The input field is empty"
})
}
});
router.delete('/todos/:id', (req, res, next) => {
Todo.findOneAndDelete({"_id": req.params.id})
.then(data => res.json(data))
.catch(next)
})
module.exports = router;
JavaScript:
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const routes = require('./routes/api');
const path = require('path');
require('dotenv').config();
const app = express();
const port = process.env.PORT || 5000;
//connect to the database
mongoose.connect(process.env.DB, { useNewUrlParser: true })
.then(() => console.log(`Database connected successfully`))
.catch(err => console.log(err));
//since mongoose promise is depreciated, we overide it with node's promise
mongoose.Promise = global.Promise;
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use(bodyParser.json());
app.use('/api', routes);
app.use((err, req, res, next) => {
console.log(err);
next();
});
app.listen(port, () => {
console.log(`Server running on port ${port}`)
});