小编Dan*_*man的帖子

承诺正在等待中

我的nodejs应用程序中有一个类,代码如下:

var mongoose    = require('mongoose');
var Roles       = mongoose.model('roles');
var Promise     = require("bluebird");

module.exports = Role;

var err = null;
var id;

function Role(name, companyId) {
    this.err = err;
    this.name = name;
    this.companyId = companyId;
    this.id = getId(name, companyId);
}



var getId = function (name, companyId) {
     return new Promise(function(resolve, reject) {
        Roles.findOne({companyId:companyId, name:name}, function(err,result) {
              resolve(result._id);
        });
     });
};
Run Code Online (Sandbox Code Playgroud)

当我在调用类时,id正在等待:

var currentRole = new Role(myRole, comId);
console.log(currentRole);
Run Code Online (Sandbox Code Playgroud)

如何在解决时从类中获取值?

node.js promise bluebird

7
推荐指数
1
解决办法
5327
查看次数

每个用户的单独猫鼬模式

我是 nodejs 的新手。我有一个带有 mongoose 和用户登录的应用程序。mongoDB 中的一些集合是针对所有用户的,而另一些则例如 customer-collection 是单独的,因此所有用户都有自己的客户集合。当用户登录并获取他的 jwt-token 时,各个猫鼬模型将被实例化:

var models = require('../models')(userID);
Run Code Online (Sandbox Code Playgroud)

在文件夹 /models 中有一个 index.js:

var Customer = require('./customermodel'); 

module.exports = function (userID) {
   Customer(userID) 
}
Run Code Online (Sandbox Code Playgroud)

文件/models/customermodel.js:

var mongooseCustomer = function(userID) {
    var schema = mongoose.Schema({
          _id: { type: Number},
          Name:  { type: String },
          Address:  { type: String },
        }, {collection: userID + 'userID'})
        return mongoose.model(userID + 'customer', schema);
}
Module.exports = mongooseCustomer;
Run Code Online (Sandbox Code Playgroud)

当客户从两个不同的浏览器登录并返回错误时会出现问题:无法覆盖123customer模型一旦编译。我通过改变解决了这个问题:

var models = require('../models')(userID);
Run Code Online (Sandbox Code Playgroud)

到:

try {
     var models = …
Run Code Online (Sandbox Code Playgroud)

mongoose node.js express mongoose-schema

2
推荐指数
1
解决办法
2465
查看次数

如何使用nodejs和express设置授权头

我按照本教程设置了一个包含nodejs,express,mongoose和swig模板的站点: 使用JSON Web令牌验证Node.js API

在本教程中,作者使用Postman在标题中设置标记.我已经谷歌搜索了几天,以了解如何在我的网站的标题中设置jwt标记,但它不适合我.

mongoose node.js express jwt swig-template

1
推荐指数
1
解决办法
7627
查看次数