Why may a mongoose document not get saved?

I’ve been using this middleware for sometime, then suddenly it stops working.

var body = req.body;

new ordersModel (body).save(function(err, newOrder, rowCount) {
	if (err) throw err;
	console.log(rowCount) // logs 1

	updateEstimatedTime(newOrder); /* this function runs but inside
   it, trying to find this same document returns null,
   meaning it was never saved */

If I try something like

var pleaseWork = new ordersModel (body);
console.log(pleaseWork) // dumps a loaded mongoose object
pleaseWork.save(function(err, newOrder, rowCount) {

So why doesn’t it get persisted? I also tried reloading the server several times with this at the top just in case

ordersModel.find({}, 'customer', function (err, docs) {
	if (err) throw err;

But it just returns the documents that existed before this fault began. I’ve triple checked the model name, it’s the same all over the script. What could the code be lacking?
I have seen some examples also that suggested something in the region of

var pleaseWork = new ordersModel ();
pleaseWork.foo = body.foo;
pleaseWork.bar = body.bar;
pleaseWork.john = body.john;

console.log(pleaseWork) // dumps a loaded mongoose object
pleaseWork.save(function(err, newOrder, rowCount) {

Now, this is not feasible for me; the ordersModel table has more than 10 columns-- dropping the body object in the model’s constructor is sane enough and worked the last time the program was run.

It’s probably just a typo, but you have an extra space between the model name and the parens.

1 Like

Did you mean this

should be changed to this new ordersModel(body)? Cuz I tried that and the situation hasn’t changed.

Yup, space is superfluous.

Ok. Well, if you can make a bare bones example that demonstrates your problem (that I can run on my machine), I don’t mind having a look.

The code in the OP is what my server uses. I can’t retype cuz I’ve run out of battery power. But this is the first time I’m hearing space between characters can mean something different in JavaScript.

It’ll still probably work, but is superfluous.

Anyway, as I said, if you can make something that reproduces your problem, that I can run on my machine then I’ll happily take a look. Good luck!

Well, in a sense, you cannot reproduce. That middleware is massive so I carved out the relevant portion which is what you’re seeing currently. The project is on github tho, see if you can deploy it. Try to place an order, assign to yourself etc. Authentication details are at the top of the file

Most of the files and folders there should be in the public directory. I uploaded them separately since I can’t upload whole folders through github web interface. I thought I could rename them later but much to my surprise, there’s no option for folder manipulation. What you might do is to create a pull request if you can move them so I can merge. Thank you.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.