小编das*_*cht的帖子

如何在RethinkDB中创建唯一项

如何在RethinkDB中创建唯一项?

在MongoDb中我使用ensureIndex了这个,例如:

userCollection.ensureIndex({email:1},{unique:true},function(err, indexName){
Run Code Online (Sandbox Code Playgroud)

rethinkdb

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

在保存到Mongoose之前清理数据

我正在尝试创建一个预处理程序,在写入MongoDB之前清理所有数据,请参阅:http://mongoosejs.com/docs/middleware.html

我尝试过以下方法让每个房产都能清理它:

  blogSchema.pre('save', function (next) {
        var obj = this;
        console.log(obj)//-> https://gist.github.com/daslicht/70e0501acd6c345df8c2

        // I've tried the following to get the single items :
        Object.keys(obj).forEach(function (key) {
            console.log('Keys: ',obj[key]);
        });

        //and:
        for(var key in obj) {
            console.log(obj[key])
        }

        //and:
        _.each( self , function(value, key, list){
            console.log('VALUE:',key);
       })
        next();
    })
Run Code Online (Sandbox Code Playgroud)

上述任何一种方法都会产生如下情况:

这是输出:

    for(var key in obj) {
       console.log(obj[key])
    }
Run Code Online (Sandbox Code Playgroud)

https://gist.github.com/daslicht/cb855f53d86062570a96

有谁知道如何获得每个单一的财产,以便我可以消毒它,好吗?

〜马克

[编辑]这是一个可能的解决方法,无论如何,将它直接放在Scheme级别会更干净,因为这样会更干

        var post = {
            createdAt : req.body.date,
            createdBy : req.user.username,
            headline : req.body.headline,
            content : req.body.content …
Run Code Online (Sandbox Code Playgroud)

mongoose node.js express

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

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

ExpressJS Middleware req,res,下一个范围

在研究了一些中间件后,我还有一个问题.

看看下面的工作设置,它只是将do it函数附加到req对象,以便我们可以在任何路径中调用它 req.doit()

但是,req,res,next来自哪里?,我从未通过它们,我更加好奇它是如何工作的,因为匿名函数(2.)被另一个函数(1.)包围,我甚至可以传递参数.

MiddleWareTest.js:

var test = function(options){ //1.)
    return function(req, res, next) { //2.)
        req.doit = function() {
            console.log('doit')
        }
        next();
    }
}
module.exports = test;
Run Code Online (Sandbox Code Playgroud)

app.js:

...
var myMiddleware =  require('./MiddlewareTest.js')
app.use(myMiddleware())
...
Run Code Online (Sandbox Code Playgroud)

欢迎任何加深我知识的建议:)

〜马克

connect node.js express

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