Reputation: 582
I am currently a student in the ways of Express and failure.
My current headache is that PUT isn't PUT-ting. My app has a crapton of data that I need to upload the first time a user logs in. I figured after that it'd be faster, because I could just upload only what's been changed. This theory isn't working out so well.
The steps are:
I broke 2 & 4 into two pieces, because when I did it in one, for some reason Mongoose started throwing hissy fits. Except now, it doesn't actually PUT anything at step 4, or even later when the user edits it (via a form in the app). It's like it stops listening, or something.
This is what the values schema looks like (truncated because it's HUGE):
var ValueSchema = new Schema({
user_id: {
type: String,
required: true,
unique: true
},
base: [{
type: String,
required: true
}],
value1: [ String ],
value2: [ String ],
value3: [ String ],
value4: [ String ]
});
And this is the section for GETting and PUTting:
// ------------------ FORM VALUES
router.route('/values/:user_id')
// create
.post(function(req, res) {
var task = req.body;
var value = new Value(task);
value.save(function(err) {
if (err) {
res.json(err);
} else {
res.write("success");
}
});
})
// update
.put(function(req, res) {
var values = req.body;
var user_id = req.params.user_id;
Value.update({user_id: user_id}, function(err, values) {
if (!err) {
res.json("okay");
} else {
res.write("fail");
}
});
})
// view
.get(function(req, res) {
var id = req.params.user_id;
Value.find({'user_id':id}, function(err, value) {
if (!err) {
res.send(value);
} else {
res.write("fail");
}
});
})
});
For reasons I still can't figure out, step 2 takes FOREVER. On top of that, Firefox sends messages that there's no token, yet the http call shows up as successful both in the Network tab and in the outputs from the server itself. Here's one of those http calls:
return $http({
method: 'PUT',
url: '/api/values/' + userId,
data: values,
headers: {
'x-access-token': token
}
})
.then(function (response) {
// chained to next step
// etc
Looks textbook to me, but when I check in Postman after any PUT, the values entry just looks like this:
[
{
"_id": "56513f140dab13e932682cf2",
"user_id": "5651f7454cd0c2543ca5202f",
"__v": 0,
"value1": [],
"value2": [],
"value3": [],
"value4": []
}
]
It's just not updating at all, after that first POST. The crazy thing is that I can update the other two schema and everything works fine, and those have identical patterns to the Value schema. Help! What am I missing?
thanks in advance!
Upvotes: 2
Views: 2355
Reputation: 103305
Your update()
method is missing the update document:
// update
.put(function(req, res) {
var values = req.body;
var user_id = req.params.user_id;
Value.update({user_id: user_id}, values, function(err, values) {
if (!err) {
res.json("okay");
} else {
res.write("fail");
}
});
})
Upvotes: 5