相关疑难解决方法(0)

在node.js中共享对象并避免使用全局变量

db使用我的路由器/控制器在下面的片段(变量)中共享数据库连接而不将db变量转换为全局变量的最合适方法是什么?

var mongo = require('mongoskin'),
db = mongo.db(config.db.adress);

app.use(function(req, res, next) {
    db.open(function(err, data) {
        (err) ? res.send('Internal server error', 500) : next();
    });
});

// Setting up controllers here
app.post('/users', require('./controllers/users').create);
Run Code Online (Sandbox Code Playgroud)

来自PHP背景,我开始考虑依赖注入,但我不知道这是否适合于节点.

javascript global global-variables node.js express

5
推荐指数
2
解决办法
8741
查看次数

TypeError:无法读取undefined(expressjs)的属性'findAll'

TypeError:无法读取undefined(expressjs)的属性'findAll'.

所有功能(续集)都不起作用.所有错误:无法读取属性'sequelize方法'...

module.exports = function (sequelize, DataTypes) {
var User = sequelize.define('user', {
    email: {type: DataTypes.STRING(32), unique: true, allowNull: false},
});

return User;
};
Run Code Online (Sandbox Code Playgroud)

控制器:

models  = require('./../models');

exports.index = function (request, response, next) {
    models.User.findAll({attributes: ['id', 'username']});
};
Run Code Online (Sandbox Code Playgroud)

javascript node.js express sequelize.js

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