Created
January 22, 2013 05:51
-
-
Save PaulWoodIII/4592429 to your computer and use it in GitHub Desktop.
I had a bug using express.js and backbone.js. I thought it was backbone for the longest time. It wasn't I accidentally removed a key line of code for the configuration of my app.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //If you get an error in the console like this: | |
| /* | |
| TypeError: Cannot convert null to object | |
| at exports.updateImagePost (/Projects/nodecellar_pw/routes/imageposts.js:160:12) | |
| at callbacks (/Projects/nodecellar_pw/node_modules/express/lib/router/index.js:161:37) | |
| at param (/Projects/nodecellar_pw/node_modules/express/lib/router/index.js:135:11) | |
| at param (/Projects/nodecellar_pw/node_modules/express/lib/router/index.js:132:11) | |
| at pass (/Projects/nodecellar_pw/node_modules/express/lib/router/index.js:142:5) | |
| at Router._dispatch (/Projects/nodecellar_pw/node_modules/express/lib/router/index.js:170:5) | |
| at Object.router (/Projects/nodecellar_pw/node_modules/express/lib/router/index.js:33:10) | |
| at next (/Projects/nodecellar_pw/node_modules/express/node_modules/connect/lib/proto.js:199:15) | |
| at Object.static (/Projects/nodecellar_pw/node_modules/express/node_modules/connect/lib/middleware/static.js:55:61) | |
| at next (/Projects/nodecellar_pw/node_modules/express/node_modules/connect/lib/proto.js:199:15) | |
| */ | |
| //And your code is taking in request from the request body like this: | |
| // server.js | |
| var imagepost = require('./routes/imageposts'); | |
| app.put('/imageposts/:id', imagepost.updateImagePost); | |
| // imageposts.js | |
| exports.updateImagePost = function(req, res) { | |
| var id = req.params.id; | |
| console.log('Updating imagepost: ' + id); | |
| var imagepost = req.body; | |
| console.log('Updating imagepost: ' + req.body); | |
| delete imagepost._id; | |
| console.log(JSON.stringify(imagepost)); | |
| db.collection('imageposts', function(err, collection) { | |
| collection.update({'_id':new BSON.ObjectID(id)}, imagepost, {safe:true}, function(err, result) { | |
| if (err) { | |
| console.log('Error updating ImagePost: ' + err); | |
| res.send({'error':'An error has occurred'}); | |
| } else { | |
| console.log('' + result + ' document(s) updated'); | |
| res.send(imagepost); | |
| } | |
| }); | |
| }); | |
| } | |
| //Then you probabaly need to add in this bodyParser call to your app.configure function! | |
| // server.js | |
| app.configure(function () { | |
| app.use(express.bodyParser()); | |
| }); | |
| // Theres some other middlewear options to consider as well | |
| // this sure drove me nuts! | |
| // Hope this being on the internet helps the next guy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment