'data do not insert to mongodb, recieve empty json file

i use code in app.js:

const express = require("express");
const app=express();
const _route=require('./router/router.js')
require('dotenv').config();
require('./Database/config.js');
const port_=process.env.PORT|| 8080;


app.use('/insert',async(req,res)=>{
    const newProduct=new product({
        name:'GLX SHAHIN 3',
        id:1,
        description:'Newest iranian phone'
    });
    try{
        await newProduct.save();
        res.send(`Inserted : ${newProduct}`);
    }
    catch(err){
        res.send(err);
    }
    });
app.listen(port_,'localhost',()=>{ console.log(`App run on port ${port_}`)});

and config the database as follow:

require('dotenv').config();
const mongoose=require('mongoose');
const port_=process.env.PORT|| 8080;
 const connectionString=`mongodb://localhost:${port_}:sale`;


mongoose.connect(connectionString,(err)=>{
    (err===true)? console.log('Fail to connect to mongodb'):console.log('Connect success.');
});

module.exports=mongoose;

i want insert to database. i enter localhost:3000/insert, get empty json file.The connection to the database appears to be established but data not insert to database. what is problem?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source