![](/img/trans.png)
[英]What is the difference between mongoose.Schema() and new mongoose.Schema()?
[英]Call statics in mongoose.Schema from another mongoose.Schema
我使用猫鼬模式,我有两个js文件;
first.js:
const mongoose = require('mongoose')
var FirstSchema= new mongoose.Schema({
F1: {
type: Boolean,
default: null
},
F2: {
type: Boolean,
default: null
}
})
FirstSchema.statics.add_to = function (_param) {
//DO SOMETHING
}
var First = mongoose.model('First', FirstSchema )
module.exports = {
First
}
socound.js:
const mongoose = require('mongoose')
var { First } = require('../func/first.js')
var SocoundSchema = new mongoose.Schema({
S1: {
type: Boolean,
default: null
},
S2: {
type: Boolean,
default: null
}
})
SocoundSchema.statics.add_other = function (_param) {
return new Promise((resolve, reject) => {
return First.add_to(_param).then((Result) => {
return resolve(Result);
}, (err) => {
return reject(err);
})
})
}
var socound = mongoose.model('socound', SocoundSchema )
module.exports = {
socound
}
当我调用First.add_to时,它不起作用。
我测试了不同的代码和方法,但失败了...
如何在另一个mongooseSchema的mongooseSchema中使用静态? 有解决方案吗?
在socound.js中,您的.add_other函数存在问题(缺少括号):
SocoundSchema.statics.add_other = function (_param) {
return new Promise((resolve, reject) => {
return First.add_to(_param)
.then((Result) => {
return resolve(Result);
}, (err) => {
return reject(err);
})
})
}
并且您在调用First.addd_to()时使用.then,因此它应该是一个Promise,我只是在代码中添加了async
First.js:
FirstSchema.statics.add_to = async function (_param) {
//DO SOMETHING
console.log('ayy');
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.