我的控制器文件:
'use strict'
var Project = require('../models/project');
function saveProject(req, res){
var project = new Project();
Project.find({name: project.name}, (err, project)=>{
if(err) return res.status(500).send({message: 'Error en la peticion'});
if(project && project.length >=1){
return res.status(404).send({message: 'El proyecto ya existe en la base dedatos'});
}else{
project.save((err, projectStored)=>{
if(err) return res.status(500).send({message: 'Error en la peticion'});
if(!projectStored) return res.status(404).send({message: 'No se ha podido salvar el proyecto'});
return res.status(200).send({project: projectStored});
});
}
});
}
module.exports = {
saveProject
}
我的模型文件:
'use strict'
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ProjectSchema = Schema({
name: String,
description: String,
category: String,
year: Number,
langs: String,
image: String
});
module.exports = mongoose.model('Project', ProjectSchema);
我的路线文件:
'use strict'
var express = require('express');
var projectController = require('../controllers/project');
var api = express.Router();
// Middlewares de Autenticacion
// Llamamos a los metodos y le definimos su ruta
api.post('/save-project', projectController.saveProject);
// Exportamos
module.exports = api;
答案 0 :(得分:1)
这里:
Project.find({name: project.name}, (err, project)=>{ ...
^^^^^^^ you are receiving `project` as argument
请注意,项目是参数,而不是您在上面一行定义的变量中的项目:
var project = new Project();
这一定是导致您错误预期的原因。