gpt4 book ai didi

node.js - 抛出新的 mongoose.Error.MissingSchemaError(name); ^ Mongoose 错误: Schema hasn't been registered for model "merchant"

转载 作者:太空宇宙 更新时间:2023-11-03 22:51:22 25 4
gpt4 key购买 nike

  throw new mongoose.Error.MissingSchemaError(name);
^

MongooseError:尚未为模型“商家”注册架构。使用 mongoose.model(名称,模式) 在 Mongoose.model (C:\survey-system\node_modules\mongoose\lib\index.js:362:13)

app.js:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var index = require('./routes/index');

require('./models/merchant');

var mongoose = require('mongoose').model('merchant');
var merchant = require('./routes/merchant');
mongoose.connect('mongodb://localhost/SK');

var app = express();

routes/index.js

var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/SK');
var merchant = mongoose.model('merchant');

router.get('/merchant', function(req, res, next) {
merchant.find({},function(err, docs){
res.send('index',{docs:docs})
});
});

model/merchant.js

var mongoose = require('mongoose')
var Schema = mongoose.Schema;

var merchantSchema = new Schema({

id :Number,
merchant_id :{type: [String], index: true },
merchant_name :String,
merchant_type :String,
contact_name :String,
phone :String,
email :String,
Address :String,

created_date: { type: Date, default: Date.now }
});


var merchant = mongoose.model('merchant', merchantSchema);

exports =merchant;

最佳答案

在模型导入上方声明mongoose.connect

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose')

mongoose.connect('mongodb://localhost/SK');


var MerchantModel = require('./models/merchant');

// var mongoose = require('mongoose').model('merchant');
var index = require('./routes/index');
var merchant = require('./routes/merchant');


var app = express();

关于node.js - 抛出新的 mongoose.Error.MissingSchemaError(name); ^ Mongoose 错误: Schema hasn't been registered for model "merchant",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42468201/

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