I use mongoose Schema , I've two js file;
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
}
When I call First.add_to, it does not work.
I tested different codes and methods, but failed...
How can I use the static in a mongooseSchema in another mongooseSchema? Does it have a solution?
In the socound.js there is a problem in your .add_other function(missing parentheses):
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.js:
FirstSchema.statics.add_to = async function (_param) {
//DO SOMETHING
console.log('ayy');
return true;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.