Node.js 如何在Mongoose模式方法中指定'this'

Node.js 如何在Mongoose模式方法中指定'this',node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我试图在Mongoose中对模式方法进行一些修改。我想知道如何从我正在使用的模式中调用信息,有点像使用this 我的模式如下所示: 'use strict'; var mongoose = require('mongoose'), Schema = mongoose.Schema; var BuildingSchema = new Schema({ name: String, info: String, level: {

我试图在Mongoose中对模式方法进行一些修改。我想知道如何从我正在使用的模式中调用信息,有点像使用
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);
我从控制器中调用这样的方法

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

到目前为止,我的
print in prompt
返回
undefined

这里是打印出
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
>

实际上,关键字
将永远不会返回
未定义的
,似乎您的实例方法设置不正确,或者您错过了导出模型…打印出
建筑
实例的是什么样的数据?像这样尝试:
console.log('Print in browser:',(new Building.printThis())啊,我刚刚看到我的
控制台.log('Print in browser:',building.printThis)的字符串改为“在提示符下打印”。也就是说,我从来没有接触过这种方法。谢谢你的帮助^^我很高兴它帮你解决了这个问题