lundi 20 avril 2015

Duplicate key error index Mongoose

Im trying to simply create multiple objects with empty arrays into them. Unfortunately there is an error message when i want to create more than one element like that.

Here is my object schema:

var groupSchema = mongoose.Schema({
id: mongoose.Schema.ObjectId,
name: { type: String, required: true },
section: { type: mongoose.Schema.ObjectId, ref:"Section", childPath:"groups" },
users: [ {type : mongoose.Schema.ObjectId, ref : 'User', childPath: "groups"} ],
invitations: [{
    _id:false,
    email: { type: String, required: true },
    isRegistered: { type: Boolean, required: true }
}],

});

Simple create function:

//Create new group
exports.createGroup = function(req, res){
Group.create(req.body, function(err, group){
    if(err){
        console.log(err);
        res.json(err);
        return false;
    }
    res.json({status: true, group: group});
    return true;
});
};

And error message:

{ [MongoError: insertDocument :: caused by :: 11000 E11000 duplicate key error index: skaud_up.groups.$invitations.email_1  dup key: { : null }]
name: 'MongoError',
code: 11000,
err: 'insertDocument :: caused by :: 11000 E11000 duplicate key error index: skaud_up.groups.$invitations.email_1  dup key: { : null }' }

Honestly I don't know why i cant have multiple elements with empty arrays i mnogoDB database.

Can someone explain me what is the cause of issue and what is the proper way of using this sort of objects?

how to workaround npm "Error: Invalid version: "0.1" BUG?

I am trying to build a nodejs package. When I run npm install I get Error: Invalid version: "0.1 message and npm installation fails.

I tried to fix the error manually by replacing "version": "0.1", with "version": "0.0.1", in package.json files in modules directories but there are many modules that contain invalid 0.1 version. It's very hard to fix it manually.

Is there a simpler way to fix it? Or maybe an awk, sed or other bash script that search for package.json files recursively and replace "version": "0.1", with "version": "0.0.1", help?

EDIT: I already checked out this thread npm: Why is a version "0.1" invalid? and lots of others prior to asking question

npm with nonProxyHost



I've searched over the internet what is the correct way to setup npm when you need to have some sites accessed over the proxy and some without, but haven't found an answer.

To give you example, we are having npm registry mirror within our corporate infrastructure and have to therefore access it without proxy setting. On the other hand, some npm modules are accessing public urls on their own, especially in case they need to download few things, and therefore need proxy set. Very good example is the phantomjs binary.
So having npm setup with proxy option and registry pointing to our internal infrastructure is not possible now, because when proxy is set, then all urls (even registry one) are resolved thru proxy server.

So I would need something similar like Java has:
-Dhttp.proxyHost="gate.company.com" -Dhttp.proxyPort=1234 -Dhttp.nonProxyHosts=10.\|.company.com"

Is it possible with npm?

I've checked the npm config, but there are just 'proxy' and 'https-proxy' settings, but nothing in direction I require.

Thnx for answers.

Cant use req.user in another controller

I'm using an angular-fullstack-generator with sequelize.

I have a problem, i need to use the logged User object (req.user) in another controller (with another Model), not in User controller.

But req.user in the other controller is undefined. any idea?

Thanks

Javascript Grunt passing arguments for Tasks

I'm at the beginning with Grunt and I'm facing with this issue: I've to pass as argument two paths, but, also if the execution seems to be ok, the task doesn't really work... Any hints?

grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),

    copy: {
        backup: {
            files: [
                {expand: true, src: [process.argv[2]], dest: [process.argv[3]]},
            ],
        },
    },

If I try to run

grunt copy:backup:sourcefolder:destinationfolder

but the code executes without giving any result.

Thanks in advance for the help!

nodejs' ldapauth module install failure

I have a nodejs application running on windows7 64bit. Now I want to install the ldapauth (http://ift.tt/1O5mjzZ) but when I do I get the following error during install. Please help!

C:\Programs\nodejsCloudant++>npm install ldapauth
npm WARN package.json make@0.0.0 No repository field.

> bcrypt@0.7.5 install C:\Programs\nodejsCloudant++\node_modules\ldapauth\node_modules\bcrypt
> node-gyp rebuild
C:\Programs\nodejsCloudant++\node_modules\ldapauth\node_modules\bcrypt>if not defined npm_config_node_gyp (node "C:\Programs\nodejs\node_modules\npm\bin\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild )  else (rebuild)
gyp ERR! configure error
gyp ERR! stack Error: Command failed: CreateProcessW: Access is denied.
gyp ERR! stack

Singleton MongoDB connection in Node

What is the best way to set up a singleton in Node for Mongodb? I tried the following code, but it does not work when making a lot of calls rapidly.

The singleton does not get set up before subsequent calls, and thus it tries opening too many connections and eventually fails. The below call works well for making infrequent calls.

Anyone have suggestions on the best practice here?

var db_singleon;

var getConnection= function getConnection(callback)
{
    if (db_singleton)
    { 
      callback(null,db_singleton);
    }
    else
    {
        var connURL = mongoURI; //set in env variables
        mongodb.connect(connURL,function(err,db){
            if(err)
                console.error("Error creating new connection "+err);
            else
            {
                db_singleton=db;    
                console.error("created new connection");
            }
            callback(err,db_singleton);
            return;
        });
    }
}