I'm testing the connectivity from my mongoose database to my app using GET method on Postman, but I only get this:
{
"ok": 0,
"code": 13,
"codeName": "Unauthorized",
"name": "MongoError"
}
I haven't added any authorization whatsoever to my app yet so I'm wondering what does that even mean? I went through the code multiple times looking for typos etc. but didn't find anything. I tried to Google the error but found nothing of relevance. Some people have had vaguely similar issues but they weren't related to Postman or making Angular apps or were unrelated in some other way.
This is my app.js:
const express = require("express");
const app = express();
const { mongoose } = require("./db/mongoose");
const bodyParser = require("body-parser");
const { List, Task } = require("./db/models");
app.use(bodyParser.json());
// Reittien käsittely
// Listojen reitit
app.get("/lists", (req, res) => {
List.find({})
.then((lists) => {
res.send(lists);
})
.catch((e) => {
res.send(e);
});
});
app.post("/lists", (req, res) => {
let title = req.body.title;
let newList = new List({
title,
});
newList.save().then((listDoc) => {
res.send(listDoc);
});
});
app.patch("/lists", (req, res) => {
});
app.delete("/lists", (req, res) => {
});
app.listen(3000, () => {
console.log("Serveri running on port 3000");
});
This is my index.js:
const { List } = require("./list.model");
const { Task } = require("./task.model");
module.exports = {
List,
Task,
};
mongoose.js:
const mongoose = require("mongoose");
mongoose.Promise = global.Promise;
mongoose
.connect("mongodb://localhost:27017/TaskMaister", { useNewUrlParser: true })
.then(() => {
console.log("Yhteys MongoDB:seen onnistui hienosti.");
})
.catch((e) => {
console.log("Virhe: yhteyttä MongoDB:seen ei pystytty muodostamaan.");
console.log(e);
});
mongoose.set("useCreateIndex", true);
mongoose.set("useFindAndModify", false);
module.exports = {
mongoose,
};
And list.model.js:
const mongoose = require("mongoose");
const ListSchema = new mongoose.Schema({
title: {
type: String,
required: true,
minlength: 1,
trim: true,
},
});
const List = mongoose.model("List", ListSchema);
module.exports = { List };
And task.model.js:
const mongoose = require("mongoose");
const TaskSchema = new mongoose.Schema({
title: {
type: String,
required: true,
minlength: 1,
trim: true,
},
_listId: {
type: mongoose.Types.ObjectId,
required: true,
},
});
const Task = mongoose.model("Task", TaskSchema);
module.exports = { Task };
Here's a screenshot of my folder structure:
Screenshot of folder structure
Hope you can help.