node.js - redirect after a POST to mongodb not working -
i'm going crazy!
i'm writing simple messageboard node/mongodb user can open threads , comment on them. after post /newthread, inserts data db (which works), have redirect _id of document, server crashes "cannot read posttitle of undefined".
if restart server , navigate thread (which shown on index-page, correctly inserted), can view thread without problem.
only after insert , redirect, there seems no document found. can't figure out why, can help?
//index.js router.post('/newthread', function(req, res) { threads.save(req, res); }); //threads.js var db = require('./db'); var uri = 'mongodb://localhost:27017/project'; var coll = 'documents'; var oid = require('mongodb').objectid; module.exports.loadall = function(cb) { db(uri, {}, function(err, db) { db.collection(coll, function(err, collection) { collection.find( { $query: {}, $orderby: { "bumpedat": -1 } } ).toarray(function(err, items) { console.log("found items: " + items.length); cb(null, items); }) }); }) }; module.exports.loadone = function(id, cb) { db(uri, {}, function(err, db) { db.collection(coll, function(err, collection) { if (err) { console.log(err); } else { console.log("searching id: " + id); console.log("converted oid: " + new oid(id)); collection.findone({ _id: new oid(id) }, {}, function(err, item) { if (err) { console.log("error " + err); cb(err); } else if (item) { console.log("found item!"); console.dir(item); cb(null, item); } else { console.log("found no items"); cb(err); } }) } }) }) }; module.exports.save = function(req, res) { db(uri, {}, function(err, db) { db.collection(coll, function(err, collection) { var time = new date(); console.log("saving..."); collection.insert({ "op" : req.body.username ||"anonymous", "posttitle" : req.body.posttitle || "no title", "postcontent" : req.body.postcontent, "createdat" : time, "bumpedat" : time, "comments" : [] }, function (err, doc) { if (err) { res.send ("error while saving post.") } else { console.log("saved!"); console.dir(doc); var threadid = new oid(doc._id).tostring(); res.location('/thread/' + threadid); console.log("reloacting " + threadid); res.redirect('/thread/' + threadid); console.log("redirecting " + threadid); } }) }) }) }; //thread.js (route /thread/:id) router.get('/:id', function(req, res) { console.log("loading " + req.params.id); threads.loadone(req.params.id, function(err, item) { if (err) { console.log(err); } else { res.render('showthread', { title: item.posttitle, posts: item }) } }) }); console output saving thread is:
saving... saved! {result: { ok: 1, n: 1 }, // ... } relocating 557d2305be8e753b0e0cf70a redirecting 557d2305be8e753b0e0cf70a post /newthread 302 96.992 ms - 120 loading 557d2305be8e753b0e0cf70a searching id: 557d2305be8e753b0e0cf70a converted oid: 557d2305be8e753b0e0cf70a found no items typeerror: cannot read property 'posttitle' of undefined @ [...]/thread.js:16:28 (that's @ "title: item.posttitle,")
am accessing wrong?
the id read, , correct. on index, links generated _id, totally correct. if click index link, can see thread. redirected (after post) url shows correct link (with same id), can't loadone id after posting... why??
Comments
Post a Comment