我似乎无法获取模型来创建图片类型的新对象。这是我正在使用的一些代码。
最后一点:
import { picture } from '../models/pictures'
//etc..
pics.post('/upload', (req, res) => {
upload.single('image')(req, res, (err) => {
if(err){
return res.status(500).json({ message: err });
} else {
if(req.file == undefined){
return res.status(500).json({ message: 'upload a valid file!' });
} else {
var pic = new picture({
title: req.body.title,
description: req.body.description,
filename: req.body.databasepicname,
});
res.status(500).json({ message: 'woo!' })
};
}
});
});
对于模型:
import mongoose from 'mongoose'
import User from './users'
const pictureSchema = new mongoose.Schema({
title: {type: String, maxlength: [50, 'Title must be longer than 50 characters']},
description: {type: String},
filename: {tpe: String},
user: {type: mongoose.Schema.Types.ObjectId}
});
var picture = mongoose.model('Picture', pictureSchema);
exports = picture;
非常感谢任何帮助。非常感谢。
最佳答案
在模型文件中,您提供默认导出,但在端点文件中,您需要命名导出:
// models/pictures.js
export = picture;
// endpoint.js
import { picture } from '../models/pictures';
尝试将其更改为此(在两个文件中使用命名导出):
// models/pictures.js
export.picture = picture;
// endpoint.js
import { picture } from '../models/pictures';
或者这个(在两个文件中使用默认导出):
// models/pictures.js
export = picture;
// endpoint.js
import picture from '../models/pictures';
关于javascript - x.x 不是构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56245962/