【发布时间】:2019-01-18 11:42:27
【问题描述】:
我正在尝试学习使用 node.js 制作一个 api 对于我的后端,我使用的是 mongodb,我使用的是 mongoose 作为 ORM。我按如下方式创建了我的用户模型。
// User.js
var mongoose = require('mongoose');
var UserInfoSchema = new mongoose.Schema({
street: String,
housenumber: String,
city: String,
postcode: String,
bus: String,
});
var UserFullSchema = new mongoose.Schema({
name: String,
email: String,
password: String,
Userinfo: [UserInfoSchema]
});
mongoose.model('User', UserFullSchema);
const User = mongoose.model('user',UserFullSchema)
module.exports = User;
我的用户控制器如下所示:
// UserController.js
var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');
router.use(bodyParser.urlencoded({ extended: true }));
var User = require('./User');
// CREATES A NEW USER
router.post('/', function (req, res) {
User.create({
name : req.body.name,
email : req.body.email,
password : req.body.password,
Userinfo: [{
street : req.body.street,
housenumber : req.housenumber,
city : req.body.city,
postcode : req.body.postcode,
bus : req.body.bus,
}]
},
function (err, user) {
if (err) return res.status(500).send("There was a problem adding the information to the database.");
res.status(200).send(user);
});
});
由于某种原因,当我使用邮递员调用我的 API 进行测试时,我永远看不到能够填充我的数组。我是不是做错了什么?
我在发布前检查过的页面: Page I checked
【问题讨论】:
标签: node.js mongodb express mongoose postman