Tuesday, 30 May 2017

Keep getting TypeError whenever I try to start a server with npm start using express and mongoose

Everytime I try to run my server from the command line with the command "npm start" I keep getting this error message:

 throw new TypeError('Undefined type `' + name + '` at `' + path +
    ^

TypeError: Undefined type `P` at `0`

Here are two modules that are supposedly causing it according to the message log I get in Git Bash.

Here is the first:

var mongoose = require('mongoose');
var Currency = require('mongoose-currency').loadType(mongoose);

var Schema = mongoose.Schema;

var promoSchema = Schema({

  name: {
    type: String,
    required: true,
    unique: true
  },

  image: {
    type: String,
    required: true,
    unique: true
  },

  label: {
    type: String,
    required: false,
    default: ""
  },

  price: {
    type: Currency,
    required: true
  },

  description: {
    type: String,
    required: true
  }
},
{
  timestamps: true
});

var Promotions = Schema('Promotion', promoSchema);

module.exports = Promotions;

And here is the second:

var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var promoRouter = express.Router();
var Verify = require('./verify');

var Promotions = require('../models/promotions');

promoRouter.use(bodyParser.json());

promoRouter.route('/')

        .get(Verify.verifyOrdinaryUser, function(req, res, next){
            Promotions.find({}, function(err, promotion){
                if (err) throw err;
                res.json(promotion);
            })
        })

        .post(Verify.verifyOrdinaryUser, Verify.verifyAdmin, function(req, res, next){
            Promotions.create(req.body, function (err, promotion) {
                if (err) throw err;
                console.log('Promotion created!');
                var id = promotion._id;

                res.writeHead(200, {
                    'Content-Type': 'text/plain'
                });
                res.end('Added the promotion with id: ' + id);
            })
        })

        .delete(Verify.verifyOrdinaryUser, Verify.verifyAdmin, function(req, res, next){
            Promotions.remove({}, function (err, resp) {
                if (err) throw err;
                res.json(resp);
            });
        });

promoRouter.route('/:promotionId')


    .get(Verify.verifyOrdinaryUser, function (req, res, next) {
        Promotions.findById(req.params.promotionId, function (err, promotion) {
            if (err) throw err;
            res.json(promotion);
        });
    })

    .put(Verify.verifyOrdinaryUser, Verify.verifyAdmin, function (req, res, next) {
        Promotions.findByIdAndUpdate(req.params.promotionId, {
            $set: req.body
        }, {
            new: true
        }, function (err, promotion) {
            if (err) throw err;
            res.json(promotion);
        });
    })

    .delete(Verify.verifyOrdinaryUser, Verify.verifyAdmin, function (req, res, next) {
        Promotions.findByIdAndRemove(req.params.promotionId, function (err, resp) {
            if (err) throw err;
            res.json(resp);
        });
    });

module.exports = promoRouter;



via T-Dot1992

No comments:

Post a Comment