Problem with urlshortener

Problem with urlshortener
0

#1

I am trying to complete the urlshortener challenge using the mongoose odm to communicated with a mongodb database on my machine

    var express = require('express');
    var validUrl = require('valid-url');
    var path = require('path');
    var mongodb = require('mongodb')
    var mongoose = require('mongoose');

    var app = express()

    mongoose.connect('mongodb://localhost:27017/urlshortener');

    var urlNumberSchema = new mongoose.Schema({
    	urlNumber: {
    		type: Number
    	}
    })

    var urlSchema = new mongoose.Schema({
    	originalUrl: {
    		type: String,
    		required: true
    	},
    	shortUrl: {
    		type: String,
    		required: true
    	}
    })

    var UrlNumber = new mongoose.model('UrlNumber',urlNumberSchema)

    var Url = new mongoose.model('Url',urlSchema)

    app.get('/api/:uri*',function(req,res){

        var uri = req.originalUrl;
        var actualUri = uri.split('api/')[1];
        console.log(actualUri);
        var status = validUrl.isUri(actualUri)
        console.log('status '+status);
        console.log(req.protocol);
        console.log(req.get('host'));

        res.send(status)
    })


    app.listen(3000);
    console.log('Server running on port 3000');

I have the database running on port 27107. However try as I may I get the following error

TypeError: Cannot read property 'UrlNumber' of undefined

What is the problem and how can I solve it?


#2

I’ve only just gotten started with Intro to MongoDB using Mean stack on EdX but could this be about missing quotation marks?


#3

How about removing the new in the new mongoose.model(..) lines?

Also I noticed that you have a uri* parameter in your GET route but you didn’t use it.


#4

Removing new worked! Do you have any idea why?


#5

I think that’s just how mongoose works.