[英]node mongoose parse JSON payload in request.body
我目前正在創建節點服務器,並與其他客戶端進行測試。 我想在POST請求中發送JSON,解析json並使用所述有效負載初始化一個新的mongoose.model對象,然后將其存儲在DB中。
看來這需要body-parser ,它是NPM軟件包,以前是express的一部分。 獲取內部服務器錯誤。 我還確保提供正確的標題(內容類型:application / json)。
我包括的主要代碼當前正在工作並保存到數據庫,但是我使用的是本地虛擬數據,而不是從請求正文中實例化的新用戶。 這是我要實例化新用戶的JSON。
{ "_id": 10, "email": "abc@gmail.com", "acct": "12345678", "phone": 9785551212, "cars": [], "address": { "street": "123 Main St.", "city": "boston", "state": "ma", "zip": 2903 }}
在我的閱讀中,我發現了許多可行但不可行的方法:
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(bodyParser.json({ type: 'application/*+json' }));
app.use(require('connect').bodyParser());
我嘗試過的所有方法均無濟於事,並且總是出現內部服務器錯誤。 先謝謝您的幫助。
var express = require('express');
var app = module.exports = express();
var mongoose = require('mongoose');
Schema = mongoose.Schema;
//var bodyParser = require('body-parser');
var userSchema = Schema({
_id: Number,
email: String,
acct: String,
address: {
street: String,
city: String,
state: String,
zip: Number
},
phone: Number,
cars: [{type: Schema.Types.Number, ref: "Car" }]
});
var carSchema = mongoose.Schema({
_id: Number,
_owner: {type: Number, ref: 'User'},
make: String,
model: String,
year: String,
color: String,
plate: String
});
var Car = mongoose.model('Car', carSchema);
var User = mongoose.model('User', userSchema);
var sampleUser = new User({
"_id": 10,
"email": "abc@gmail.com",
"acct": 12345678,
"name": "John Doe",
"address": {
"street": "123 Main St.",
"city": "Boston",
"state": "Ma",
"zip": "02903"
},
"phone": "9785551212"
});
var car1 = new Car({
_id : 51,
_owner: sampleUser._id,
make: "saab",
model: "9000",
year: "1997",
color: "navy",
plate: "777000"
});
app.post = function(request, response) {
var db = mongoose.connection;
mongoose.connect(process.env.MongoConnectionString);
db.on('error', function () {
response.send(500, {message: 'Failed to connect to mongo'});
});
db.once('open', function callback() {
console.log("Sucessfully Logged into mongo");
//this is the trouble line. I want to use reqest.body,
//or json.parse/json.stringify to
//parse my json and initialize a new user.
//var user = new User(request.body);
sampleUser.save(function (err) {
if (err)
return console.log('insert error');
});
//save the car to the db
car1.save(function (err) {
if (err)
return console.log('insert error');
});
//push the car into the user.cars array and save
sampleUser.cars.push(car1);
sampleUser.save();
//post response
response.send(statusCodes.OK, sampleUser.cars);
});
}
要使用request.body中的JSON負載實例化一個新的Mongoose.model對象,只需設置每個字段,如下所示。
嘗試執行類似以下操作時,會引起混亂:
var x = new X(request.body).
話雖如此,我想知道上面的代碼是否可行。
請求標頭必須具有Content-Type:application / json,並且應該存在以下正文分析器代碼。 在我的情況下,address包含幾個json字段,因此,如果您執行以下操作,似乎正文解析器應該在所有情況下都可以工作。
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
var user = new users({
_id: request.body._id,
email:request.body.email,
account:request.body.account,
name:request.body.name,
address:request.body.address,
phone:request.body.phone
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.