0

I am trying to create a User list when user submit their details. In app.js I have the function for that.

like this:

app.post('/addList', userList.submit);

I am calling my function which is nested in userList.submit:

var sendList = require('../model/model');

exports.list = function(req, res){
  res.send("respond with a resource this is from list file");
};

exports.addList = function(req, res, next){
    res.render('index', {title:'title from list.js'});
};

exports.submit = function(req,res,next){
    var name = req.body.name,
        dept = req.body.dept;

    sendList.create({
        name:name,
        dept:dept
    }, function(error){
        console.log('Erros is ', error);
        if (error) return next(error);
        res.redirect('/');
    });
};

In the model folder, model.js has the function as follows:

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/user_list');

var schema = new Schema({
    name:String,
    dept:String
});

module.exports = mongoose.model('User', schema);

But I am getting error saying:

var schema = new Schema({
                 ^
ReferenceError: Schema is not defined

What is this mean? where I made the mistake? any one figure out and help me to solve this?

2 Answers 2

1

Schema is defined as a property of mongoose. It's not there in the global scope of nodejs.

var schema = new mongoose.Schema({
    name:String,
    dept:String
});
Sign up to request clarification or add additional context in comments.

5 Comments

But I have another application, which i am using to upload the files, it works fine.. how?
It's basic programming. When you say new Schema, you need to have a javascript function (in this case a constructor) named Schema defined somewhere. How can you call a function which is not defined?
In your other app, there must be a line like var Schema = mongoose.Schema
There is no line like so. I understand that, that's a photo upload form, which stores the photos in local folder. so it may not needed. Am I correct?
Yup. I assumed you are saving the photos in mongodb gridfs since your question was on mongodb.
0

Schema is part of an application or is a property of mongoose. It has nothing to do with db, it's only mapping db into js objects. you have to change the code : = new mogoose.schema ({ what ever you want ..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.