x.x不是构造函数

时间:2019-05-21 20:33:30

标签: javascript node.js express

我似乎无法获得模型以创建图片类型的新对象。这是我正在使用的一些代码。

最后的提示:

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;

任何帮助将不胜感激。非常感谢。

1 个答案:

答案 0 :(得分:1)

在模型文件中,您将提供默认导出,但在端点文件中,您将需要命名导出:

<link rel="stylesheet" href="app/css/styles.css">

尝试将其更改为此(在两个文件中使用命名的导出):

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