From 4cd36ea3fc08b5fd8371e4835b07dfcdd2d5d226 Mon Sep 17 00:00:00 2001 From: counterweight Date: Sun, 1 Jun 2025 23:33:27 +0200 Subject: [PATCH] yiiiiha --- parts/3/followAlong/index.js | 26 +++++++++++++++++++++++++- parts/3/followAlong/mongo.js | 10 +++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/parts/3/followAlong/index.js b/parts/3/followAlong/index.js index e168b9e..649ec77 100644 --- a/parts/3/followAlong/index.js +++ b/parts/3/followAlong/index.js @@ -1,6 +1,28 @@ const express = require("express"); const app = express(); +const mongoose = require("mongoose"); + +const url = "mongodb://devroot:devroot@localhost:27017/fsopen?authSource=admin"; +mongoose.set("strictQuery", false); + +mongoose.connect(url); + +const noteSchema = new mongoose.Schema({ + content: String, + important: Boolean, +}); + +noteSchema.set("toJSON", { + transform: (document, returnedObject) => { + returnedObject.id = returnedObject._id.toString(); + delete returnedObject._id; + delete returnedObject.__v; + }, +}); + +const Note = mongoose.model("Note", noteSchema); + app.use(express.json()); let notes = [ @@ -24,7 +46,9 @@ app.get("/", (request, response) => { }); app.get("/api/notes", (request, response) => { - response.json(notes); + Note.find({}).then((notes) => { + response.json(notes); + }); }); app.post("/api/notes", (request, response) => { diff --git a/parts/3/followAlong/mongo.js b/parts/3/followAlong/mongo.js index 38c88f5..0ae8150 100644 --- a/parts/3/followAlong/mongo.js +++ b/parts/3/followAlong/mongo.js @@ -17,7 +17,15 @@ const note = new Note({ important: true, }); -note.save().then((result) => { +/* note.save().then((result) => { console.log("note saved!"); mongoose.connection.close(); +}); +*/ + +Note.find({}).then((result) => { + result.forEach((note) => { + console.log(note); + }); + mongoose.connection.close(); });