我将表单数据保存到NodeJS API,然后获取ID并将该ID保存到另一个文档。此时的表单实际上是将数据保存到我的MLab数据库,然后发送SERVER 500错误,并且没有进入console.logs进行故障排除。不知道发生了什么,因为该过程非常简单。
const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');
const Project = require('../../models/project.model');
const Client = require('../../models/client.model');
router.post('/', async (req, res, next) => {
let clientId = req.body.clientId;
let newProject = new Project({
_id: new mongoose.Types.ObjectId(),
projectTitle: req.body.projectTitle,
network: req.body.network,
postSupervisor: {
name: req.body.postSupervisor.name,
email: req.body.postSupervisor.email ? req.body.postSupervisor.email : '',
phone: req.body.postSupervisor.phone ? req.body.postSupervisor.phone : ''
},
seriesInformation: {
services: req.body.seriesInformation.services,
season: req.body.seriesInformation.season,
numEps: req.body.seriesInformation.numEps,
runtime: req.body.seriesInformation.runtime
},
technicalDetails: {
frameRate: req.body.technicalDetails.frameRate,
videoFormat: req.body.technicalDetails.videoFormat,
videoFinishing: req.body.technicalDetails.videoFinishing,
soundFormat: req.body.technicalDetails.soundFormat,
sampleRate: req.body.technicalDetails.sampleRate,
bitDepth: req.body.technicalDetails.bitDepth,
audioLoudness: req.body.technicalDetails.audioLoudness,
audioAlgorithm: req.body.technicalDetails.audioAlgorithm,
audioPeakLimit: req.body.technicalDetails.audioPeakLimit,
technicalSpec: req.body.technicalDetails.networkSpec
},
videoBudget: {
onlineBudget: req.body.videoBudget.onlineBudget,
blurBudget: req.body.videoBudget.blurBudget,
graphicsBudget: req.body.videoBudget.graphicsBudget,
videoReviewBudget: req.body.videoBudget.videoReviewBudget
},
audioBudget: {
voBudget: req.body.audioBudget.voBudget ? req.body.audioBudget.voBudget : 0,
dialogBudget: req.body.audioBudget.dialogBudget ? req.body.audioBudget.dialogBudget : 0,
soundEffectsBudget: req.body.audioBudget.soundEffectsBudget ? req.body.audioBudget.soundEffectsBudget : 0,
soundDesignBudget: req.body.audioBudget.soundDesignBudget ? req.body.audioBudget.soundDesignBudget : 0,
foleyBudget: req.body.audioBudget.foleyBudget ? req.body.audioBudget.foleyBudget : 0,
audioMixBudget: req.body.audioBudget.audioMixBudget ? req.body.audioBudget.audioMixBudget : 0,
audioReviewBudget: req.body.audioBudget.audioReviewBudget ? req.body.audioBudget.audioReviewBudget : 0,
audioStemPrintBudget: req.body.audioBudget.audioStemPrintBudget ? req.body.audioBudget.audioStemPrintBudget : 0,
audioSecondaryBudget: req.body.audioBudget.audioSecondaryBudget ? req.body.audioBudget.audioSecondaryBudget : 0
}
});
try {
// Save new project
let project = await newProject.save();
// Get new project ID to save into the client document
let projectId = project._id;
//Update client using new project _id
let updatedClient = await Client.findByIdAndUpdate(clientId, {
projects: projectId
}, {
new: true
});
console.log(INFORMATION);
console.log(project);
console.log(updatedClient);
res.status(200).json(project);
} catch (error) {
res.status(500).json({
message: 'Server Error',
Error: error
})
}
});
module.exports = router;
答案 0 :(得分:0)
尝试一下,它应该可以正常工作。我们使用mongoose.Type。在定义模式时或在数据库中保存其实例时的ObjectId。您只需要存储其他模型实例ID作为参考,以后就可以使用它来检索foregin对象 更换 _id:新的mongoose.Types.ObjectId() 通过 型号名称:req。身体。 ID
const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');
const Project = require('../../models/project.model');
const Client = require('../../models/client.model');
router.post('/', async (req, res, next) => {
let clientId = req.body.clientId;
let newProject = new Project({
// this below line has to change
// _id field is auto increment in database
model_name_id: req.body.id ,//model_name: new mongoose.Types.ObjectId()
projectTitle: req.body.projectTitle,
network: req.body.network,
postSupervisor: {
name: req.body.postSupervisor.name,
email: req.body.postSupervisor.email ? req.body.postSupervisor.email : '',
phone: req.body.postSupervisor.phone ? req.body.postSupervisor.phone : ''
},
seriesInformation: {
services: req.body.seriesInformation.services,
season: req.body.seriesInformation.season,
numEps: req.body.seriesInformation.numEps,
runtime: req.body.seriesInformation.runtime
},
technicalDetails: {
frameRate: req.body.technicalDetails.frameRate,
videoFormat: req.body.technicalDetails.videoFormat,
videoFinishing: req.body.technicalDetails.videoFinishing,
soundFormat: req.body.technicalDetails.soundFormat,
sampleRate: req.body.technicalDetails.sampleRate,
bitDepth: req.body.technicalDetails.bitDepth,
audioLoudness: req.body.technicalDetails.audioLoudness,
audioAlgorithm: req.body.technicalDetails.audioAlgorithm,
audioPeakLimit: req.body.technicalDetails.audioPeakLimit,
technicalSpec: req.body.technicalDetails.networkSpec
},
videoBudget: {
onlineBudget: req.body.videoBudget.onlineBudget,
blurBudget: req.body.videoBudget.blurBudget,
graphicsBudget: req.body.videoBudget.graphicsBudget,
videoReviewBudget: req.body.videoBudget.videoReviewBudget
},
audioBudget: {
voBudget: req.body.audioBudget.voBudget ? req.body.audioBudget.voBudget : 0,
dialogBudget: req.body.audioBudget.dialogBudget ? req.body.audioBudget.dialogBudget : 0,
soundEffectsBudget: req.body.audioBudget.soundEffectsBudget ? req.body.audioBudget.soundEffectsBudget : 0,
soundDesignBudget: req.body.audioBudget.soundDesignBudget ? req.body.audioBudget.soundDesignBudget : 0,
foleyBudget: req.body.audioBudget.foleyBudget ? req.body.audioBudget.foleyBudget : 0,
audioMixBudget: req.body.audioBudget.audioMixBudget ? req.body.audioBudget.audioMixBudget : 0,
audioReviewBudget: req.body.audioBudget.audioReviewBudget ? req.body.audioBudget.audioReviewBudget : 0,
audioStemPrintBudget: req.body.audioBudget.audioStemPrintBudget ? req.body.audioBudget.audioStemPrintBudget : 0,
audioSecondaryBudget: req.body.audioBudget.audioSecondaryBudget ? req.body.audioBudget.audioSecondaryBudget : 0
}
});
try {
// Save new project
let project = await newProject.save();
// Get new project ID to save into the client document
let projectId = project._id;
//Update client using new project _id
let updatedClient = await Client.findByIdAndUpdate(clientId, {
projects: projectId
}, {
new: true
});
console.log(INFORMATION);
console.log(project);
console.log(updatedClient);
res.status(200).json(project);
} catch (error) {
res.status(500).json({
message: 'Server Error',
Error: error
})
}
});
module.exports = router;