如何在Mongoose Schema方法中指定`this`

时间:2022-02-07 19:40:51

I've tried to fiddle a bit around with Schema methods in Mongoose. And I was wondering how I could call informations from the schema I'm using, kinda like using this.

我试图在Mongoose中使用Schema方法。我想知道如何从我正在使用的模式调用信息,有点像使用它。

My Schema looks like this:

我的架构看起来像这样:

'use strict';

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

var BuildingSchema = new Schema({
    name: String,
    info: String,
    level: {                                //  The current level of the template, default value is 1
        type: Number,
        default: 1
    },
    ressource: {                            //  Ressouces
        level: [{
            gain: [{                        //  Gain per level
                amount: Number,
                ressource: {
                    type: Schema.ObjectId, 
                    ref: 'Ressource'
                }
            }],
            cost: [{                        //  Cost per level
                amount: Number,
                ressource: {
                    type: Schema.ObjectId, 
                    ref: 'Ressource'
                }
            }]
        }]
    },
    storage: {                              //  Storage
        capacity: [{                        //  Storage capacity changes per level
            inside: Number,
            outside: Number
        }],
        stored: {                           //  Stored
            inside: [{                      //  Ressources stored inside
                amount: Number,
                ressource: {
                    type: Schema.ObjectId, 
                    ref: 'Ressource'
                }
            }],
            outside: [{                     //  Ressources stored outside
                amount: Number,
                ressource: {
                    type: Schema.ObjectId, 
                    ref: 'Ressource'
                }
            }]
        }
    }
},
{
    toObject: { virtuals: true },
    toJSON: { virtuals: true }
});

/**
 * Methods
 */
BuildingSchema.methods = {
    printThis: function() {
         console.log('Print in prompt : ', this);
    }
};

module.exports = mongoose.model('Building', BuildingSchema);

I call the method like this from my controller

我从我的控制器中调用这样的方法

console.log('Print in browser : ', building.printThis);

So far my print in prompt returns undefined

到目前为止,我的提示中的print返回undefined

1 个解决方案

#1


Here what printed out the printThis method:

打印出printThis方法的内容如下:

> building = new Building()
{ _id: 554899217377c9b97c54bb36,
  storage: { stored: { outside: [], inside: [] }, capacity: [] },
  ressource: { level: [] },
  level: 1,
    id: '554899217377c9b97c54bb36' }
> building.printThis()
Print in prompt :  { _id: 554899217377c9b97c54bb36,
  storage: { stored: { outside: [], inside: [] }, capacity: [] },
  ressource: { level: [] },
  level: 1,
    id: '554899217377c9b97c54bb36' }
undefined
>

#1


Here what printed out the printThis method:

打印出printThis方法的内容如下:

> building = new Building()
{ _id: 554899217377c9b97c54bb36,
  storage: { stored: { outside: [], inside: [] }, capacity: [] },
  ressource: { level: [] },
  level: 1,
    id: '554899217377c9b97c54bb36' }
> building.printThis()
Print in prompt :  { _id: 554899217377c9b97c54bb36,
  storage: { stored: { outside: [], inside: [] }, capacity: [] },
  ressource: { level: [] },
  level: 1,
    id: '554899217377c9b97c54bb36' }
undefined
>