2

I wrote a little of code to start learning MongoDB with Node.js in JavaScript but it doesn't work but it doesn't give a error while running probably someone can help me with that.

The Main code:

const mongoose = require('mongoose');
const express = require('express');
const test = express();
const Blog = require('./bdSchema');


//connec to mongodb
const dbURI = 'mongodb+srv://Carsten:<int the real code the password is here>@cluster0.w6iwv.mongodb.net/tomhatesgeschaft?retryWrites=true&w=majority';
mongoose.connect(dbURI, { useNewUrlParser: true, useUnifiedTopology: true })
    .then((result) => console.log("connected to db"))
    .catch((err) => console.log(err))



test.get('/add-tomhatesgeschaft', (req, res) => {
    const tomhatesgeschaft = new Blog({
        title: 'hi'
    });
    tomhatesgeschaft.save()
        .then((result) => {
            res.send(result)
        })
        .catch((err) => {
            console.log(err);
        });
})

The BDSchema:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const BDSchema = new Schema({
    title: {
        type: String,
        required: true,
    },
});

const Blog = mongoose.model('Blog', BDSchema);
module.exports = Blog;
5
  • How are you running this code? Does it log connected to db and are you able to request get handler? Commented Jun 20, 2021 at 15:36
  • Yes it logs connected to dB Bit what does the second question mean Commented Jun 20, 2021 at 15:43
  • Open the browser and run `http:\\localhost:3000/add-tomhatesgeschaft, what does it send? Commented Jun 20, 2021 at 15:48
  • When you Namen the Right url than ther is on localhost Website with this url Commented Jun 20, 2021 at 15:54
  • Can you give more details of the situation? What do you have now? What is the expected result? Commented Jun 20, 2021 at 18:24

1 Answer 1

1

In your Node.js code, change the save function as below.

    tomhatesgeschaft.save((err,result) => {
    if (err){
        console.error(err);
    }
    else{
        res.send(result)
    })
})

OR

test.get('/add-tomhatesgeschaft', async (req, res) => {
    const tomhatesgeschaft = new Blog({
        title: 'hi'
    });
    try {
      await tomhatesgeschaft.save()
    } catch(error) {
      console.error(error)
    }
})

PS: You should not use GET requests for operations such as saving data to the database. Ideally, you should use a POST request.

Example with async:

test.post('/add-tomhatesgeschaft', async (req, res) => {
    const tomhatesgeschaft = new Blog({
        title: 'hi'
    });
    try {
      await tomhatesgeschaft.save()
    } catch(error) {
      console.error(error)
    }
})
Sign up to request clarification or add additional context in comments.

3 Comments

Hört does is it Look when i‘m using post
Edited my answer. Try following articles like this to get a clear idea on how to develop RESTful APIs
so i tried all of your solutions but none of them were working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.