JavaScript:无法将新信息POST到mongodb REST API



我一直致力于构建一个REST API。API的目的是在本地服务器上以.JSON格式显示有关太阳系行星的信息。我使用Postman测试URL(localhost:8000/planets)。我可以发出GET请求,但POST请求有问题。

我在Postman上输入localhost:8000/planets,并发出GET请求以显示我的信息。然后,我将请求类型更改为POST,然后按Send。然后我点击"Body",选择"Raw",然后将格式从"Text"更改为"JSON(application/JSON)"。

然后,我输入要添加到数据库中的数据,然后按SEND。现在,当我进行第二次GET请求时,我可以看到我刚刚发布的数据。然而,如果我对保存POST和GET请求等代码的JavaScript文件进行修改,然后在Postman上进行第三次GET请求,我刚刚发布的数据就会从数据库中消失。我对这一切都是新手,但我觉得这不应该发生。如果有问题,我相信它一定在我的代码中。我的代码是:

var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var app = express();
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
mongoose.Promise = global.Promise;
var promise = mongoose.connect('mongodb://localhost/planet_server', {
useMongoClient: true,
});
promise.then(function(db){
console.log('DATABASE CONNECTED!!');
}).catch(function(err){
console.log('CONNECTION ERROR', err);
});
var Schema = mongoose.Schema;
var planetSchema = new Schema({
name: String,
position: String,
moons: String,
diameterInKm: String,
daysInYear: String,
temperature: String
})
var Planet = mongoose.model('Planet', planetSchema);
Planet.collection.drop();

//*****RAW DATA*****
var firstEntry = new Planet({name: "Mercury",
position: "Mercury is the first planet in the solar system.",
moons: "Mercury has no moons",
diameterInKm: "Mercury is 5175km in diameter",
daysInYear: "A year on Mercury is a mere 88 days long",
temperature: "The surface temperature of Mercury is a scorching 427C!"
});
firstEntry.save(function (err) {
if (err) {
return (err);
}
});
var secondEntry = new Planet({name: "Venus",
position: "Venus is the second planet in the solar system.",
moons: "Venus has no moons",
diameterInKm: "Venus is 12,715km in diameter",
daysInYear: "A year on Venus is 225 days long",
temperature: "The surface temperature of Venus is a scorching 468C, hot enough to melt lead!"
});
secondEntry.save(function (err) {
if (err) {
return (err);
}
});
var thirdEntry = new Planet({name: "Earth",
position: "Earth is the third planet in the solar system.",
moons: "Earth has two moons.  No, seriously.  I saw it on QI.",
diameterInKm: "Earth is 12,742km in diameter",
daysInYear: "A year on Earth is 365.25 days long.  That extra 0.25 is why we have leap years",
temperature: "The surface temperature of Earth...well, depends where you live, I guess."
});
thirdEntry.save(function (err) {
if (err) {
return (err);
}
});
var fourthEntry = new Planet({name: "Mars",
position: "Mars is the fourth planet in the solar system.",
moons: "Mars has two moons, Phobos and Deimos",
diameterInKm: "Mars is 6,779km in diameter",
daysInYear: "A year on Mars is 687 days long.",
temperature: "The surface temperature of Mars is a bracing -55C"
});
fourthEntry.save(function (err) {
if (err) {
return (err);
}
});
var fifthEntry = new Planet({name: "Jupiter",
position: "Jupiter is the fifth planet in the solar system.",
moons: "Jupiter has 67 moons!",
diameterInKm: "Jupiter is 139,822km in diameter",
daysInYear: "A year on Jupiter is 4380 days long. If I lived on Jupiter, I wouldn't even be 3 years old.",
temperature: "The surface temperature of Jupiter is -145C."
});
fifthEntry.save(function (err) {
if (err) {
return (err);
}
});
var sixthEntry = new Planet({name: "Saturn",
position: "Saturn is the sixth planet in the solar system.",
moons: "Saturn has 62 moons, 5 less than Jupiter, and is very bitter about it",
diameterInKm: "Saturn is 116,464km in diameter",
daysInYear: "A year on Saturn is 10,759 days long.",
temperature: "The surface temperature of Saturn is -168C."
});
sixthEntry.save(function (err) {
if (err) {
return (err);
}
});
var seventhEntry = new Planet({name: "Uranus",
position: "Uranus is the seventh planet in the solar system.",
moons: "Uranus has 5 moons",
diameterInKm: "Uranus is a relatively svelte 50,724km in diameter",
daysInYear: "A Year on Uranus is 30,660 days long.",
temperature: "The surface temperature of Uranus is -216C.  That's colder than liquid nitrogen."
});
seventhEntry.save(function (err) {
if (err) {
return (err);
}
});
//*****END OF RAW DATA*****
app.get('/planet', function(req, res){
console.log(1);
Planet.find({}).exec(function(err, planet){
console.log(2);
if(err) {
return res.status(500).send(err);
}
return res.json(planet);
});
});
app.post('/planet', function(req, res){
var newPlanet = new Planet(req.body);
newPlanet.save(function(err, planet) {
if(err) {
return res.status(500).send(err);
}
return res.status(201).json(planet);
});
});
app.listen(8000, function(){
console.log('Listening');
});

在我最近的一次尝试中,我试图添加海王星的数据。它似乎起了作用,因为正如我所说,当我在Postman上发出POST请求后发出GET请求时,我可以看到海王星的数据以及所有其他行星的数据。然而,我注意到JavaScript中有一个小的拼写错误。我更正了它并保存了JavaScript文件,当我发出下一个GET请求时,Neptune数据不见了。我很困惑,所以如果您能提供任何帮助,我们将不胜感激。

这是因为当您重新加载js文件时,您正在运行行

var Planet = mongoose.model('Planet', planetSchema);
Planet.collection.drop();

然后再次添加您的初始数据。所以你张贴的数据丢失了。

不确定您是如何托管节点文件的,如果您在更改文件时使用nodemon或其他东西自动刷新服务器,但这正是导致数据丢失的原因,因为它正在重新运行drop命令。

最新更新