我面临着猫鼬.save()函数的问题。
Index.js
var mongoose = require('mongoose');
var companySchema = rootRequire('models/company');
mongoose.connect('mongodb://localhost:27017/test');
var company = new companySchema({activate: false, company_code: '123', name: 'A123' });
console.log(company);
company.save(function(err){
if(err){
console.log("now it can be associated with db",err);
}
else{
console.log("bingo");
}
});我的控制台日志输出是
{激活: false,_id: 582997952a3134cc08672607,名称:A 123, company_code:'123‘}
我没有收到任何日志
Console.log(“现在它可以与db相关联”,err);
或
Console.log(宾果);
我的company.js看起来就像
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var companySchema = new Schema({
name: {
type: String,
required: true,
sparse: true,
unique: true
},
company_code: {
type: String,
required: true
},
activate: {
type: Boolean,
default: false
},
logo: {
type: String
}
}, {
collection: 'company'
});
var Company = mongoose.model('company',companySchema)
module.exports = Company;发布于 2016-11-14 11:50:49
使用单个文件解决了我的问题,但不可行。
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
//Company Schema
var companySchema = new Schema({
name: {
type: String,
required: true,
sparse: true,
unique: true
},
company_code: {
type: String,
required: true
},
activate: {
type: Boolean,
default: false
},
logo: {
type: String
}
}, {
collection: 'company'
});
var Company = mongoose.model('company',companySchema)
//console.log(Company);
mongoose.connect('mongodb://localhost:27017/test');
var company = new Company({activate: false, company_code: '123', name: 'OSPL3' });
console.log(company);
company.save(function(err){
console.log('comses');
if(err){
console.log("now it can be associated with db",err);
}
else{
console.log("bingo");
}
});是否遇到了与rootRequire相关的问题?
发布于 2016-11-14 11:55:57
我尝试执行您的代码,我刚刚在index.js中添加了一行var rootRequire = require('root-require');,它对我来说很好。
发布于 2016-11-14 12:38:35
在模式设计之前应包括猫鼬。
如果你看一下我以前的答案和代码,它会更清楚
https://stackoverflow.com/questions/40587384
复制相似问题