具有关系的模式未保存在数据库中

问题描述 投票:0回答:3

[我正在使用基本操作操作创建一个api,我正在使用mongodb,并且我有1个需要从另一个模式获取信息的模式,我是通过这种方式建立关系的,在第一个shema中我就有了。

const mongoose = require('mongoose');
const { Schema } = mongoose;

const geographicalAreaSchema = new Schema({
    name: {type: String, required: true}
})

module.exports = mongoose.model('GeographicalArea', geographicalAreaSchema);

带有参考的第二个模式

const mongoose = require('mongoose');
const { Schema } = mongoose;

const zooSchema = new Schema({
    name: { type: String, required: true },
    nit: { type: String, required: true },
    geographicalArea: { type: Schema.ObjectId, ref: "GeographicalArea" }
})

module.exports = mongoose.model('Zoo', zooSchema);

在控制器中具有此代码以保存新的动物园

const zoo = require('../models/zoo');
const mongoose = require('mongoose');
const GeographicalArea = mongoose.model('GeographicalArea');
const zooController = {};

zooController.createZoo = async(req, res) => {
    const saveZoo = new zoo({
        name: req.body.name,
        nit: req.body.nit,
        geographicalAreaName: req.body.geographicalArea.name //this item have data
    });
    await saveZoo.save()
    res.json({
        'status': 'Zoo Save'
    })
};

module.exports = zooController;

这是路由器文件

const express = require('express');
const router = express.Router();

const zoo = require('../controllers/zoo.controller');

router.post('/', zoo.createZoo)

module.exports = router;

这是索引文件

const express = require('express');
const morgan = require('morgan');
const app = express();

const { mongoose } = require('./database');

//Settings
app.set('port', process.env.PORT || 3000);

//Middlewares
app.use(morgan('dev'));
app.use(express.json());

//Routes
app.use('/api/geographicalArea', require('./routes/geographicalArea.routes'));
app.use('/api/zoo', require('./routes/zoo.routes'));

//Starting server
app.listen(app.get('port'), () => {
    console.log('Server on port', app.get('port'))
})

当我向邮递员发出这样的请求时

postman

我看req是否带来数据

enter image description here

并且我调试以验证数据是否正确保存,geographicArea的字段返回未定义

enter image description here

我不知道为什么该属性未定义

node.js mongodb express crud database-schema
3个回答
0
投票
尝试一下

const mongoose = require('mongoose'); const { Schema } = mongoose; const zooSchema = new Schema({ name: {type: String, required: true }, nit: { type: String, required: true }, geographicalArea: { type: Schema.Types.ObjectId, ref: "GeographicalArea" } }); module.exports = mongoose.model('Zoo', zooSchema);


0
投票
尝试这种方法。

const zoo = require('../models/zoo') const mongoose = require('mongoose') const GeographicalArea = mongoose.model('GeographicalArea') const zooController = {} zooController.createZoo: async (req, res) => { try { const zooInfo = { name: req.body.name, nit: req.body.nit, geographicalAreaName: req.body.geographicalArea.name } const zooSave = await zoo.create(zooInfo) console.log(zooInfo) zooSave.save() res.json({ status: "zoo saved "}) } catch(error) { res.json({ error }) } }


0
投票
通过从邮递员仅发送我想与动物园相关的geoArea id来解决此问题,这样我就可以获取包含所有信息的对象。

发送数据

enter image description here

zooController.createZoo = async(req, res) => { const saveZoo = new zoo({ name: req.body.name, nit: req.body.nit, geographicalArea: req.body.geographicalArea }); await saveZoo.save() res.json({ 'status': 'Zoo Save' }) };

我确认我得到了数据

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.