Getting error for app.get request on url shortener project

Tell us what’s happening:

I have been doing a project in URL shortening and i am getting an undefined as a result to get request. Also i get a blank page too as result,but according to my knowledge everything is ok,i can’t figure out what is the mistake Here’s my code( please check the app.get section )



use strict';
var bodyParser = require('body-parser')
var express = require('express');
var mongo = require('mongodb');
var mongoose = require('mongoose');
var http = require("http");
var cors = require('cors');
const dns = require('dns'); 
  
var app = express();

// Basic Configuration 
var port = process.env.PORT || 3000;

/** this project needs a db !! **/ 
// mongoose.connect(process.env.DB_URI);

app.use(cors());

/** this project needs to parse POST bodies **/
// you should mount the body-parser here

app.use('/public', express.static(process.cwd() + '/public'));

app.get('/', function(req, res){
  res.sendFile(process.cwd() + '/views/index.html');
});

  
// your first API endpoint... 
app.get("/api/hello", function (req, res) {
  res.json({greeting: 'hello API'});
});
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });
var saveSchema = new mongoose.Schema({
  name: String,
  url: Number,
 
});

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

/** 3) Create and Save a Person */
var SaveData = mongoose.model('Save', saveSchema);

//**Here's the start of my problem,i think**
app.get("/api/shorturl/:id1",function(req,res){
 SaveData.find({url:1},function(err,data){  console.log(data.name)//**i am getting undefined for this in console**
res.json(data.name);})
});
app.post("/api/shorturl/new",(req,res)=>{
var body=req.body.url;
dns.lookup(body,(err,data)=>{
  var new2= new SaveData({name:body,url:1});
new2.save((err,data)=>{res.json(new2);});
})
});

app.listen(port, function () {
  console.log('Node.js listening ...');
});

I checked my DB whether the schema data is inputted or not, it is getting inside DB, so retrieval makes the problem I think.

Hello!

You have some problems, but I’ll start with the one you’re asking.

Here:

SaveData.find({ url: 1 }, function(err,data) {
  console.log(data.name);
  res.json(data.name);
});

The data is being retrieved, but you’re expecting an object while the result is an array. If you want a single result, then change find to findOne or limit the results and return the first element:

SaveData.find({ url: 1 }, function(err, data) {
  return res.json(data.length > 0 ? data[0] : {});
}

Or

SaveData.find({ url: 1 }, function(err, data) {
  return data;
});

The other problems:

  1. dns.lookup expects the parameter (you’re passing body) to be only the domain part (i.e, freecodecamp.org or forum.freecodecamp.org), so you need to figure out a way to retrieve that.
    • You’re ignoring the err parameter of the dns.lookup, hence you’ll never know when a URL is valid or not.
  2. You’re ignoring every err, how would you know if the query succeeds or fails? Take them into account and respond accordingly :slight_smile:.
  3. I suppose you’re just testing, but in case you’re not, you should not hard code the values. SaveData.find({ url: 1 }...) has a hard coded value.

I hope it helps :slight_smile:,

Happy coding!