gpt4 book ai didi

javascript - 为什么它向我抛出错误 : TypeError: project. save is not a function

转载 作者:行者123 更新时间:2023-12-03 01:06:34 27 4
gpt4 key购买 nike

我的 Controller 文件:

'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;

最佳答案

这里:

Project.find({name: project.name}, (err, project)=>{ ...
^^^^^^^ you are receiving `project` as argument

请注意,项目是参数,而不是您在上面一行定义的变量中的项目:

var project = new Project();

这一定是你错误期望的原因。

关于javascript - 为什么它向我抛出错误 : TypeError: project. save is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52375683/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com