1
votes

I am trying to build a Node JS + Express web application using Parse.com. I have this in my server js:

var express = require('express'),
    app = express(),
    bodyParser = require('body-parser'),
    Parse = require('parse').Parse;
    projects = require('./controllers/projects');

// Parse fun
Parse.initialize('MY_APP_ID_HERE', 'MY_JS_KEY');

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

app.get('/projects',projects.index);
app.get('/projects/new',projects.new);

app.listen(9000);

Now in my projects controller:

module.exports.create = function(req,res){
    var projectObject = Parse.Object.extend("ProjectObject");
    var project = new projectObject();
    project.save(req.body).then(function(object) {
        res.redirect('/projects');
    });
};

module.exports.index = function(req,res){
    var projectObject = Parse.Object.extend("ProjectObject");
    var query = new Parse.Query(projectObject);
    query.limit(100);
    query.first({
        success: function(projects) {
            // Successfully retrieved the projects.
            res.json(projects);
        },
        error: function(error) {
            console.log("Error: " + error.code + " " + error.message);
        }
    });
};

This is the error I get:

ReferenceError: Parse is not defined at module.exports.create ([REST_OF_PATH_REMOVED]/app/controllers/projects.js:5:22) at Layer.handle [as handle_request] ([REST_OF_PATH_REMOVED]/app/node_modules/express/lib/router/layer.js:95:5) at next ([REST_OF_PATH_REMOVED]/app/node_modules/express/lib/router/route.js:131:13) at Route.dispatch ([REST_OF_PATH_REMOVED]/app/node_modules/express/lib/router/route.js:112:3) at Layer.handle [as handle_request] ([REST_OF_PATH_REMOVED]/app/node_modules/express/lib/router/layer.js:95:5) at [REST_OF_PATH_REMOVED]/app/node_modules/express/lib/router/index.js:277:22 at Function.process_params ([REST_OF_PATH_REMOVED]/app/node_modules/express/lib/router/index.js:330:12) at next ([REST_OF_PATH_REMOVED]/app/node_modules/express/lib/router/index.js:271:10) at [REST_OF_PATH_REMOVED]/app/node_modules/body-parser/lib/read.js:121:5 at done ([REST_OF_PATH_REMOVED]/app/node_modules/body-parser/node_modules/raw-body/index.js:233:14)

2

2 Answers

0
votes

Simple answer would be to move

var Parse = require('parse').Parse;

and

Parse.initialize('MY_APP_ID_HERE', 'MY_JS_KEY');

to your projects controller. They aren't necessary in your server.js file based on the code posted.

0
votes

Parse is out of the fn's scope. You could either bring the declaration to your controller as recommended before or (although not a good practice) make Parse a global (just be careful by not overwriting the variable in other parts of your app)

app.js

...

global.Parse = require('parse').Parse;

...

controller.js

...

new Parse.query ...

...