简体   繁体   English

Mongoose model 无法调用其函数

[英]Mongoose model unable to call its functions

I can't get Mongoose to use its functions我无法让 Mongoose 使用它的功能

var boxSchema = new mongoose.Schema({
    identify: String,
    name: String,
    phone: String,
});

var Box = mongoose.model("Box", boxSchema);
Box.create({
    identify: "Foo",
    name: "",
    phone: "",
},(err,box)=>{
    if(err){
        console.log(err);
    }
    else{
        console.log(`box created! ${box}`);
    }
})

It seems a bit silly.似乎有点傻。 Looks like everything is in place but the create method won't even trigger.看起来一切就绪,但 create 方法甚至不会触发。 Everything is imported up top一切都是进口的

const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const mongoose = require('mongoose');
const multer = require('multer');
const GridFsStorage = require('multer-gridfs-storage');
const Grid = require('gridfs-stream');
const methodOverride = require('method-override');

My connection with the db is done like this:我与数据库的连接是这样完成的:

const mongoURI = 'mongodb://localhost:27017/depo';
const conn = mongoose.createConnection(mongoURI, {
    useNewUrlParser: true,
    useUnifiedTopology: true
});

I also have a gridfs storage and multer implementation but it doesn't seem to meddle with the app.我也有一个 gridfs 存储和 multer 实现,但它似乎并没有干扰应用程序。 Any idea why it might not work?知道为什么它可能不起作用吗? :/ :/

Ok I actually found the problem.好的,我实际上发现了问题。 There is this conn object that I'm creating in order to use the GridFS can not be used as a mongodb connection aswell.为了使用 GridFS,我正在创建这个 conn object 也不能用作 mongodb 连接。 So i had to add a mongoose.connect around there aswell.所以我不得不在那里添加一个 mongoose.connect 。

mongoose.connect("mongodb://localhost/depo", {
    useNewUrlParser: true,
    useUnifiedTopology: true
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM