Hi all,
I’m working on the backend/API course, which will be my third FCC course that I’ve taken. I’m having a few issues, related to the exercise tracker, that I hvaen’t seen yet. I know that one challenge in microservices is keeping up with the always changing landscape of packages, hoping the issues are related. Short recap of what is going on:
I was almost done with solving the first criterion, “You can POST to /api/users with form data username to create a new user.” Somewhere DLing packages, etc, I started to get lots of error that I was assuming were an issue with my Database, or package versions, or something else under the hood:
throw err;
^
Error: Cannot find module 'express'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/app/server.js:1:17)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
2nd Attempt, copy a new Glitch project, import from the Github starting project. The second I added const mongoose = require('mongoose') after DLing the mongodb and mongoose packages, I got similar errors. I made sure to require the mongoose and mongo db versions that were in the previous exercise (url shortener), not knowing how much these courses keep up with node and mongo updates, to no avail.
3rd attempt, today, from scratch. No errors, everything on the debug window looks right, however my mongoose.connection.readyState keeps giving me a ‘2’, which means ‘connecting’, haven’t seen a 1 yet, and every time I try to put data in api/users, I just get ‘’'cannot POST /api/users```.
So, I’m getting a little frustrated, not sure what I’m doing wrong. Here are the relevant code snippets:
server.js:
const app = express()
const cors = require('cors')
require('dotenv').config()
const shortId = require('shortid')
app.use(cors())
app.use(express.static('public'))
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/index.html')
});
//DB connection
const mongoose = require('mongoose');
const myUriCode = 'mongodb+srv://<username>:<pass>@free-code-camp-3.vhtmx.mongodb.net/myFirstDatabase?retryWrites=true&w=majority';
const myUri = process.env.MONGO_URI;
mongoose.connect(myUriCode, { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true });
const listener = app.listen(process.env.PORT || 3000, () => {
console.log('Your app is listening on port ' + listener.address().port)
})
//body-parser
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
//Schema, Model
const Schema = new mongoose.Schema({
shortId: {type: String, unique: true, default: shortId.generate},
username: String,
exercise: [{
desc: String,
duration: Number,
date: {}
}]
});
const exerDb = mongoose.model('exerDb', Schema);
//Check Mongoose connection
console.log(mongoose.connection.readyState, (err, data) => {
if(err) {
console.log("the error is " + err)
} else {
console.log("the data is " + data)
}
});
//trying separate function to create person
const createAPerson = (name, done) => {
exerDb.findOne({username: name}, (err,findData) => {
if (findData == null) {
const person = new exerDb({username: name, exercise: []});
exerDb.save((err, data)=> {
if(err) {
done(err);
}
done(null, data);
});
} else if (err) {
done(err);
} else {
done(null, "taken")
}
})
};
//"You can POST to /api/users with form data username to create a new user."
app.post('api/users/:username', (req,res) => {
console.log("hello");
});
package.json:
"name": "fcc-exercise-tracker",
"version": "0.1.0",
"description": "A REST API project, part of Free Code Camp's curriculum",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"dotenv": "^8.2.0",
"express": "^4.16.4",
"cors": "^2.8.5",
"mongodb": "^3.6.0",
"mongoose": "^5.4.0",
"shortid": "^2.2.16"
},
"repository": {
"url": "https://github.com/freeCodeCamp/boilerplate-project-exercisetracker"
},
"license": "MIT",
"keywords": [
"node",
"express"
]
}
TIA!!