0

My /chat route works well through Post method with validation with Joi schema but when I send request through Get method, it show Sending Request and continue loading... My index.js file:

const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const morgan = require('morgan');

const chat = require('./db/ChatModel');

const app = express();

app.use(bodyParser.json());

app.get('/chat', (req, res) => {
    chat.getAllMessages().then( (messages) => {
        res.json(messages);
    });
});

app.post('/chat', (req, res) => {
    console.log(req.dody);
    chat.createMessages(req.body).then((message) => {
        res.json(message);
    }).catch( (error) => {
        res.status(500);
        res.json(error);
    });
});

const port = process.env.PORT || 8888;
app.listen(port, () => {
    console.log(`Listening on port ${port}...`);
});

In connection.js I coded this

const monk = require('monk');
const connectionString = 'localhost/chatboard';
const db = monk(connectionString);

module.exports = db;

And ChatModal.js has the following code

const Joi = require('joi');
const db = require('./connection');

const schema = Joi.object().keys({
    username: Joi.string().alphanum().min(4).max(16).required(),
    subject: Joi.string().required(),
    message:Joi.string().max(300).required(),
    imgUrl: Joi.string().uri({
        scheme: [      // https://github.com/hapijs/joi/blob/v14.3.1/API.md#stringurioptions
          /https?/
        ]
      })
});

const chat = db.get('chat');

function getAllMessages() {
    return chat.find();
};

function createMessages(message) {
    const result = Joi.validate(message, schema);
    if (result.error == null) {
        message.created = new Date();
        return chat.insert(message);
    } else {
        return Promise.reject(result.error);
    }
}

module.exports = {
    createMessages,
    getAllMessages
};

I can't understand why getAllMessages() doesn't work and postman continue loading when Get request applied like this http://prntscr.com/s0d9c5

mdmostafa
  • 618
  • 9
  • 19

3 Answers3

1

ChatModal.js

function getAllMessages() {
 try {
  return chat.find();
 } catch (err) {
return next(err);

}

index.js

app.get('/chat', (req, res, next) => {
 try{
    data = chat.getAllMessages()
 } catch (err) {
    return next(error);
  }
    res.json(data);

});

tapu74
  • 1,081
  • 10
  • 14
  • Yes, @tapu74! this works now `app.get('/chat', (req, res, next) => { try{ messages = chat.getAllMessages() } catch (err) { return next (err) } res.json(messages); });` So `get` will not support `.then()` function, right? Because it works on `app.post` but `app.get` and thank you very much for your support – mdmostafa Apr 16 '20 at 09:50
0

User try-catch in the ChatModal.js and also index.js then you can understand what is actual error, like bellow:

ChatModal.js

function getAllMessages() {
  try {
    chat.find();
  } catch (err) {
  return next(err);
}
tapu74
  • 1,081
  • 10
  • 14
  • Yes, now error shows like `TypeError: Cannot read property 'then' of undefined at ... index.js:23:26` In `index.js`, I used `.then()` twice on `app.get` and `app.post`. `Post` works well but `Get` doesn't work and error shows as `TypeError` on `.then()` – mdmostafa Apr 16 '20 at 09:11
  • `app.get('/chat', (req, res) => { chat.getAllMessages().then((messages) => { res.json(messages); }); });` For this code, error shows `TypeError: Cannot read property 'then' of undefined` – mdmostafa Apr 16 '20 at 09:15
  • Yes, `app.get` works now on postman but now `app.post` is loading that worked before and when loading after submitting `post` request, `undefined` shows on terminal against `console.log(req.body);` on `index.js` – mdmostafa Apr 16 '20 at 15:02
  • **index.js** `app.post('/chat', (req, res) => { console.log(req.dody); chat.createMessages(req.body).then((message) => { res.json(message); }).catch( (error) => { res.status(500); res.json(error); }); });` – mdmostafa Apr 16 '20 at 15:09
  • similar to get, you don't need then in the post also. https://stackoverflow.com/questions/3884281/what-does-the-function-then-mean-in-javascript – tapu74 Apr 16 '20 at 15:37
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/211817/discussion-between-mostafa-bd-and-tapu74). – mdmostafa Apr 16 '20 at 15:39
0

I think, may be your data, i mean message list data so weight, in this case you get all message,res.json(messages); json method have long time to parse messages data

Luyen
  • 37
  • 1
  • 11