[英]Call statics in mongoose.Schema from another mongoose.Schema
I use mongoose Schema , I've two js file; 我使用猫鼬模式,我有两个js文件;
first.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 : 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
}
When I call First.add_to, it does not work. 当我调用First.add_to时,它不起作用。
I tested different codes and methods, but failed... 我测试了不同的代码和方法,但失败了...
How can I use the static in a mongooseSchema in another mongooseSchema? 如何在另一个mongooseSchema的mongooseSchema中使用静态? Does it have a solution?
有解决方案吗?
In the socound.js there is a problem in your .add_other function(missing parentheses): 在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);
})
})
}
and you are using .then when calling First.addd_to() so its supposed to be a Promise, i just added async to the code 并且您在调用First.addd_to()时使用.then,因此它应该是一个Promise,我只是在代码中添加了async
First.js: First.js:
FirstSchema.statics.add_to = async function (_param) {
//DO SOMETHING
console.log('ayy');
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.