我无法让猫鼬使用其功能
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}`);
}
})
似乎有点傻。看起来一切就绪,但是create方法甚至不会触发。 一切都从头开始导入
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');
我与数据库的连接是这样完成的:
const mongoURI = 'mongodb://localhost:27017/depo';
const conn = mongoose.createConnection(mongoURI, {
useNewUrlParser: true,
useUnifiedTopology: true
});
我也有gridfs存储和multer的实现,但是它似乎与应用程序无关。 知道为什么它可能行不通吗? :/
答案 0 :(得分:0)
尝试一下:
var boxSchema = new mongoose.Schema({
identify: {
type: String
},
name: {
type: String
},
phone: {
type: String
}
});
var Box = mongoose.model("Box", boxSchema);
var BoxModel = new Box();
BoxModel.identify = "Foo";
BoxModel.name = "";
BoxModel.phone = "";
BoxModel.save();
答案 1 :(得分:0)
好吧,我实际上发现了问题。我正在创建此conn对象以使用GridFS,它也不能用作mongodb连接。所以我也必须在附近添加一个mongoose.connect。
mongoose.connect("mongodb://localhost/depo", {
useNewUrlParser: true,
useUnifiedTopology: true
});