gpt4 book ai didi

node.js - 无法让 PUT 在 Nodejs/express 上工作

转载 作者:IT老高 更新时间:2023-10-28 23:24:47 28 4
gpt4 key购买 nike

我一直在编写我的第一个 Node 应用程序,一个 rest api。我的一切工作都很好,但 PUT 永远不会通过。我尝试了很多不同的组合,很多旧教程和一些新教程,我​​可能非常接近我只是不确定如何更新记录。另外,如果您看到任何突然出现在您身上的东西,请随时告诉我!我真的很喜欢 node,所以我很想学习最佳实践。

下面是到我的 table 的路线,所有其他端点都工作正常。如果您有任何问题,请告诉我。

//Database
var mongoose = require("mongoose");
mongoose.connect('mongodb://Shans-MacBook-Pro.local/lantern/');


// Schema
var Schema = mongoose.Schema;
var Person = new Schema({
first_name: String,
last_name: String,
address: {
unit: Number,
address: String,
zipcode: String,
city: String,
region: String,
country: String
},
image: String,
job_title: String,
created_at: { type: Date, default: Date.now },
active_until: { type: Date, default: null },
hourly_wage: Number,
store_id: Number,
employee_number: Number

});
var PersonModel = mongoose.model('Person', Person);


// Return all people
exports.allPeople = function(req, res){
return PersonModel.find(function (err, person) {
if (!err) {
return res.send(person);
} else {
return res.send(err);
}
});
}


// Create A Person
exports.createPerson = function(req, res){
var person = new PersonModel({
first_name: req.body.first_name,
last_name: req.body.last_name,
address: {
unit: req.body.address.unit,
address: req.body.address.address,
zipcode: req.body.address.zipcode,
city: req.body.address.city,
region: req.body.address.region,
country: req.body.address.country
},
image: req.body.image,
job_title: req.body.job_title,
hourly_wage: req.body.hourly_wage,
store_id: req.body.location,
employee_number: req.body.employee_number
});

person.save(function (err) {
if (!err) {
return console.log("created");
} else {
return res.send(err);
}
});

return res.send(person);
}


// Return person by id
exports.personById = function (req, res){
return PersonModel.findById(req.params.id, function (err, person) {
if (!err) {
return res.send(person);
} else {
return res.send(err);
}
});
}


// Delete a person by id
exports.deletePerson = function (req, res){
return PersonModel.findById(req.params.id, function (err, person) {
return person.remove(function (err) {
if (!err) {
console.log("removed");
} else {
return res.send(err);
}
});
});
}


// Update a person by id
exports.updatePerson = function(req, res){
var person = new PersonModel({
first_name: req.body.first_name
});

var upsertData = person.toObject();

console.log(req.params.id); // OK

delete upsertData.id;

person.update({ _id: req.params.id }, upsertData, { multi: false }, function(err) {
if(err) { throw err; }

console.log('updated visit: '+ req.params.id);

res.redirect('/');
});


}

我的 app.js

// Node Modules
var express = require('express');
var app = express();
app.port = 3000;



// Routes
var people = require('./routes/people');



// Node Configure
app.configure(function(){
app.use(express.bodyParser());
app.use(app.router);
});



// Start the server on port 3000
app.listen(app.port);



/*********
ENDPOINTS
*********/

// People
app.get('/people', people.allPeople); // Return all people
app.get('/people/:id', people.personById); // Return person by id
app.post('/people', people.createPerson); // Create A Person
app.put('/people/:id', people.updatePerson); // Update a person by id, not working
app.delete('/people/:id', people.deletePerson); // Delete a person by id



// Logs
// - Build some logs here for info when the server is going.
// - routes that are loaded, any activity that might be helpful
console.log('Server started on port ' + app.port);

最后,put 上的 json 错误

{
"name": "MongoError",
"err": "Mod on _id not allowed",
"code": 10148,
"n": 0,
"connectionId": 91,
"ok": 1
}

最佳答案

坚持了下来,终于弄明白了!这是我的 PUT 函数。

app.put('/people/:id', function (req, res) {
var person = new PeopleModel({
first_name: req.body.first_name,
last_name: req.body.last_name,
// etc etc
});

var upsertData = person.toObject();

delete upsertData._id;

return PeopleModel.update({ _id: req.params.id }, upsertData, {upsert: true}, function(err) {
if (!err) {
return res.send("updated");
} else {
console.log(err);
return res.send(404, { error: "Person was not updated." });
}
});
});

我将尝试弄清楚如何自动检测需要更新哪些字段,就像我存储了一个 var 一样进行测试。

关于node.js - 无法让 PUT 在 Nodejs/express 上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18754942/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com