Home > database >  How can I make a post request inside another request using node js, express
How can I make a post request inside another request using node js, express

Time:11-28

I made an application to make push notifications and I succeeded in sending notifications. But I have a problem, which is that I want to save any notification that I send in my database,

Here is the code,

var FCM = require("fcm-node");
const express = require("express");
const mongoose = require("mongoose");
require("dotenv/config");

const app = express();

app.use(express.json());


const notificationSchema = mongoose.Schema({
  name: String,
});

const NotificationModel = mongoose.model("Notification", notificationSchema);



app.post("/fcm", async (req, res, next) => {
  try {
    let fcm = new FCM(process.env.SERVER_KEY);
    let message = {
      to: req.body.token,
      notification: {
        title: req.body.title,
        body: req.body.body,
      },
    };

    fcm.send(message, function (err, response) {
      if (err) {
        next(err);
      } else {
        // res.json(response);
        // res.send(message.notification.body);
        app.post("/notfs", async (req, res) => {
          let newNotf = new NotificationModel({
            name: message.notification.body,
          });

          newNotf = await newNotf.save();
          res.send(newNotf);
        });
      }
    });
  } catch (error) {
    next(error);
  }
});

app.get("/notfs", async (req, res) => {
  const notfs = await NotificationModel.find();
  res.send(notfs);
});

mongoose
  .connect(process.env.CONNECTION_STRING)
  .then(() => {
    console.log("connected");
  })
  .catch((err) => {
    console.log(err);
  });

app.listen(3000, () => {
  console.log("listened");
});

Why doesn't it save notifications in the database?

Another question Please if there is a better way than this please leave it and thank you٫ Thanks in advance

CodePudding user response:

use axios package, which is recommended by nodejs official. Its simple like jquery ajax call

CodePudding user response:

try that

var FCM = require("fcm-node");
const express = require("express");
const mongoose = require("mongoose");
require("dotenv/config");

const app = express();

app.use(express.json());

console.log(process.env.CONNECTION_STRING);

const notificationSchema = mongoose.Schema({
  name: String,
});

const NotificationModel = mongoose.model("Notification", notificationSchema);

app.get("/notfs", async (req, res) => {
  const list = await NotificationModel.find();
  res.send(list);
});

app.post("/fcm", async (req, res, next) => {
  let newNotf = new NotificationModel({
    name: req.body.body,
  });

  try {
    let fcm = new FCM(process.env.SERVER_KEY);
    let message = {
      to: req.body.token,
      notification: {
        title: req.body.title,
        body: req.body.body,
      },
    };

    fcm.send(message, async function (err, response) {
      if (err) {
        next(err);
      } else {
        res.json(response);
        newNotf = await newNotf.save();
      }
    });
  } catch (error) {
    next(error);
  }
});

mongoose
  .connect(process.env.CONNECTION_STRING)
  .then(() => {
    console.log("connected");
  })
  .catch((err) => {
    console.log(err);
  });

app.listen(3000, () => {
  console.log("listened");
});

  • Related