为什么我的剩余api发布请求为什么不将我的数组保存在mongodb数据库中

时间:2018-08-10 20:22:58

标签: node.js mongodb express mongoose postman

我正在尝试学习使用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看起来像这样:

// 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进行测试时,我再也看不到能够填充我的数组。我做错什么了吗?

图片为例: enter image description here

我在发布前检查的页面: Page I checked

3 个答案:

答案 0 :(得分:0)

您想要在_id处进行交叉引用的定义似乎已经消失。

Userinfo: [UserInfoSchema]

应该是:

Userinfo: [{ type: mongoose.Schema.Types.ObjectId, ref:  'UserInfoSchema' }]

这样,您将得到_id的交叉引用。顺便说一句,不是那些名字的粉丝。它看起来很混乱。我认为这些只是用于测试。

答案 1 :(得分:0)

现在,从简单的<paths> <files-path name=”databases” path=”/” /> </paths> 角度来看,您是否甚至还在引用req.body中的值? 值不应该为express吗?

req.body.Userinfo.street

在旁注中,您可以使用邮递员发送完整的JSON,转到Userinfo: [{ street : req.body.Userinfo.street, // housenumber : req.body.Userinfo.housenumber, city : req.body.Userinfo.city, postcode : req.body.Userinfo.postcode, bus : req.body.Userinfo.bus, }] 标签,然后从下拉列表中选择raw

答案 2 :(得分:0)

我不知道问题出在哪里,但是我可以通过将模型分成2个不同的文件来解决此问题。 我这样创建了一个名为Userinfo的新文件:

// Userinfo.js
var mongoose = require('mongoose');  
var UserInfoSchema = new mongoose.Schema({
    street: String,
    housenumber: String,
    city: String,
    postcode: String,
    bus: String,
  });

module.exports = UserInfoSchema;

我在那里导出了整个架构

然后在我的用户模型文件中将该模式称为:

// User.js

var Userinfo = require('../userinfo/Userinfo.js');
var mongoose = require('mongoose');  
var UserFullSchema = new mongoose.Schema({  
  name: String,
  email: String,
  password: String,
  Userinfo: [Userinfo]
});

const User = mongoose.model('user',UserFullSchema)
module.exports = User;

我的控制器如下所示:

// UserController.js
var express = require('express');
var mongoose = require('mongoose');
var router = express.Router();
var bodyParser = require('body-parser');
router.use(bodyParser.urlencoded({ extended: true }));
var User = require('./User');


router.post('/', function (req, res) {
    User.create({
        name : req.body.name,
        email : req.body.email,
        password : req.body.password,
        Userinfo: [{
            street : req.body.Userinfo.street,
            housenumber : req.body.Userinfo.housenumber,
            city : req.body.Userinfo.city,
            postcode : req.body.Userinfo.postcode,
            bus : req.body.Userinfo.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);
            console.log(req);
        });
});

此后,我像以前一样使用邮递员,问题就解决了: https://stackblitz.com/edit/angular-fugo9e?file=styles.css