Affichage des articles dont le libellé est Active questions tagged node.js - Stack Overflow. Afficher tous les articles
Affichage des articles dont le libellé est Active questions tagged node.js - Stack Overflow. Afficher tous les articles

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;
        });
    }
}

How to make multiple http calls from the nodejs server with a common callback?

I want to make multiple http calls in nodejs sever with common callback. Is there any modules available for doing it?

understanding require() with module.exports with javascript and browserify

I am a c++ programmer at heart and I'm currently being thrown into the deep end with javascript and asked to swim very quick. I am using browserify so I am able to use the require function that node.js uses to get get access to code in other files. Now everything I have done seems to be working fine, I am just unsure that I am doing it correctly.

//a.js
module.exports = function(){
    alert("hello world");
}

//b.js
var myClass = new MyClass();
module.exports = myClass;

//c.js
var a = require("./a.js");
a();
var b = require(./b.js");
b.prototype.test = 555;

//d.js
function () {
    var a = require("./a.js");
    a();
    var b = require(./b.js");
    assert(b.test === 555);
}
function () { // duplicated require called inside same file but different function
    var a = require("./a.js");
    a();
}

So in every function and every file I want to use a.js do I have to put the require call? Seems like it will get a bit convoluted. Is there a better way to do this? Also assuming that c.js is ran before d.js will the assert pass or does it result in a copy being created of myClass so the objects are different between C and D?

Thanks for any help.

apr_socket_recv: connection reset by peer (104) nodejs Ubuntu 14.10 x64 8GB RAM 4 Core (VPS)

I am working on a project in node.js. The project is about location(GPS) tracking. The ultimate target for my project is to server 1M concurrent requests. What i have done are,

Created a server in node.js listing on port 8000
A html document with google map to locate user positions/GPS locations
A single socket connection between server and html document to pass location information
An API to get user location from client devices ( it can be a mobile app )
Once a server receives user location via the API mentioned it will emit that information to client (HTML document) via socket.

Its working well and good.

Problem
I am using apachebench for load test my server. When i increase the concurrency benchmarking breaks frequently with the error

apr_socket_recv: Connection reset by peer (104)  

how can i solve this, Whats the actual cause of this problem.

Note: if i run the same server in my local windows machine it serving 20K requests successfully.

I have changed the

ulimit -n 9999999

and the soft and hard file openings limit to 1000000

neither solves my problem.
please help me to understands the problem clearly. How i increase the concurrency to 1M. is it possible with some more hardware/network tunings?

Edit:

I am using socketCluster on the server with the no of worker equivalent to the no of Core/CPU (i.e) 4 workers in my case
The CPU usage with the htop command in server terminal is 45%
Memory usage was around 4GB / 8GB & swap space not used
The ab command i used to load the server was

ab -n 20000 -c 20000 "http://IP_ADDRESS:8000/API_INFO"

How do i render a data on android from node.js/mongodb server

The post method works successfully, it is just that I cannot see the string that I am sending from android to the server showing in the server

This is my code

protected String doInBackground(String... params) {
   postData(params[0]);
   return null;
   }

    @Override
    protected void onPostExecute(String s) {
        pb.setVisibility(View.GONE);
        Toast.makeText(getApplicationContext(), "Your Post has been successfully posted",
                Toast.LENGTH_SHORT).show();
    }

    public void postData(String titleToSend){
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://ift.tt/1HloeL3");

    try{
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("title", titleToSend));
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = client.execute(post);
    }catch (ClientProtocolException e){
        e.printStackTrace();
    }catch (IOException e){
        e.printStackTrace();
    }
}

The onClick listener code

public void onClick(View v) {
            String tit = null;

            if(title.getText().toString().length()<1){
                Toast.makeText(getApplicationContext(), "Please Fill in all fields", Toast.LENGTH_SHORT).show();
            }else{
                pb.setVisibility(View.VISIBLE);
                myTask task = new myTask();
                task.execute(title.getText().toString());
            }
}

The data-type name in the server is title, so what the user input in the EditText should go to the title field.

The backend api.js

api.route('/')
        // GET method to get list of ads
        .get(function(req, res) {
            Ad.find({}, function(err, ads) {
                if(err) {
                    res.send(err);
                    return;
                }
                res.json(ads);
            });
        }) 

        // POST method to post an ad to the database
        .post(function(req, res) {
            var ad = new Ad({
                title: req.body.title,
                photos: req.body.photos,
            });

Can anyone point to me what I am doing wrong here or guide me on how should I do it please

Express js: How to download a file using POST request

When I use GET, everything works fine. However, I struggle to use POST to achieve the same effect. Here are the code I have tried:

1.

app.post("/download", function (req, res) {
    res.download("./path");
});

2.

app.post("/download", function (req, res) {
    res.attachment("./path");
    res.send("ok");
});

3.

app.post("/download", function (req, res) {
    res.sendFile("./path");
});

None of them work. What is the correct way to do this?

EDIT: I submit a POST request through a HTML form to /download. ./path is a static file. When I use code in method 1, I can see the correct response header and response body in the developer tool. But the browser does not prompt a download.

Sequelize hooks not fired

I'm trying to use Sequelize beforeCreate hook to create Card instance for my User. For some reason hook is not fired:

"use strict";

module.exports = function(sequelize, DataTypes) {
  var User = sequelize.define("User", {
    email: DataTypes.STRING,
    hash: DataTypes.STRING(1024),
    salt: DataTypes.STRING(512),
    activationKey: DataTypes.STRING,
    resetPasswordKey: DataTypes.STRING
  }, {
    hooks: {
      beforeCreate: function(user, options, cb) {
        console.log('[ORM]: hook `beforeCreate` for model User...')
        models.Card
          .create({
            name: user.name,
            url: null
          })
          .then(function(card) {
            user.addCard(card).then(function() {
              cb(null, user)
            })
          })
          .catch(function(err) {
            console.log(err)
            cb(null, user)
          })
      }
    },
    classMethods: {
      associate: function(models) {
        // associations can be defined here
        User.hasMany(models.Card)
      }
    }
  });

  return User;
};

Testing:

db.sequelize.sync().then(function() {
  console.log('[INFO]: database synced')
  User.create({
    email: "helloworld@example.com",
    name: "John Dow"
  })
});

User is created, but Card — not, hook is not fired:

[INFO]: database synced
Executing (default): INSERT INTO "Users" ("id","email","updatedAt","createdAt") VALUES (DEFAULT,'helloworld@example.com','2015-04-20 09:22:50.388 +00:00','2015-04-20 09:22:50.388 +00:00') RETURNING *;

Any ideas, what I'm doing wrong? Thank you!

async.waterfall bind context

I am currently working on a web application with node.js and I can't figure a context problem with the library async.

Here is a example of code of my application :

notification.prototype.save = function (callback) {

    async.parallel([
        // Save the notification and associate it with the doodle
        function _saveNotification (done) {

            var query = 'INSERT INTO notification (notification_id, user_id, doodle_id, schedule_id) values (?, ?, ?, ?)';
            notification.db.execute(query, [ this.notification_id, this.user_id, this.doodle_id, this.schedule_id ], { prepare : true }, function (err) {
                return done(err);
            });

            console.log("SAVE NOTIFICATION");
            console.log("doodle_id", this.doodle_id);

        }.bind(this),

        // Save notification for the users with the good profile configuration
        function _saveNotificationForUsers (done) {
            this.saveNotificationForUsers(done);
        }.bind(this)

    ], function (err) {
        return callback(err);
    });
};

So in this code, I have to use the bind method to bind the context of my object ( this ) because otherwise async change it. I got it. But what I don't understand is why the code of this.saveNotificationForUsers does not work the same way :

notification.prototype.saveNotificationForUsers = function (callback) {

    console.log("SAVE NOTIFICATION FOR USERS");
    console.log("doodle id : ", this.doodle_id);

    async.waterfall([
        // Get the users of the doodle
        function _getDoodleUsers (finish) {
            var query = 'SELECT user_id FROM users_by_doodle WHERE doodle_id = ?';
            notification.db.execute(query, [ this.doodle_id ], { prepare : true }, function (err, result){
                if (err || result.rows.length === 0) {
                    return finish(err);
                }

                console.log("GET DOODLE USERS");
                console.log("doodle id : ", this.doodle_id);

                return finish(err, result.rows);
            });
        }.bind(this)
    ], function (err) {
        return callback(err);
    });
};

When I call the previous code, the first console.log is able to show me the "this.doodle_id" variable, which means the function knows the "this" context. But the functions inside the waterfall call does not, even if I bind 'this' to them.

I figured a way to make it works by creating a 'me' variable which is equal to 'this' juste before I call waterfall, and by binding the functions with the 'me' variable' and not this, but I would like to understand why I am forced to do this when I use async.waterfall and not when I use async.parallel.

I hope I was clear with the description of my problem, if someone can help me understand it will be a great pleasure !

Meteor JS: include dynamic name template in layout

I have this basic layout. I want to include a dynamic header to be included in the template. +header should be like this +{{get_header_name}}. get_header_name is a helper function. I tried this idea but jade will throw an error. Any ideas how to make it dynamic?

basic.jade

template(name="basicLayout")
    #main
        header
            +header // <--- make this a dynamic using helper (get_header_name)
            +search
        else
            +yield
        footer
            +footer

What is a fast & scalable way of identifying nearby defined set of places?

Given a set of places with lat/long predefined (locations that are not defined in google maps etc should be included), I need to get the top N number of places which are within a given distance radius from a given location (also with lat/long defined). An example for the places list would be restaurants in a country. Considering processing time and scalability (assuming the number of prefined places will grow over time) what is the best way to do this?

To narrow things down assume the places list and the location can be filtered by an area code. The location will be sent from a mobile to a NodeJS Express service where I'm hoping to do the processing on the server end for returning a list of nearby places back.

I found the link below which calculates direct distance between two lat/long points but need to know if there is a better way since I may need to consider driving distance too.

http://ift.tt/1DCVkUf

Mocha-casperjs is not creating the xunit file

I am using mocha-casperjs for running my tests. I'm running my tests with the command make test. Thereby my Makefile looks like:

Makefile:

test:
    # Clear console log before we start.
    @clear

    # Make sure we are not having too much modules.
    @npm prune

    # Make sure we have the required modules.
    @npm install

    # Clear the console, so we only see the test results.
    @clear

    # Run the test.
    ./node_modules/.bin/mocha-casperjs sample.js --xunit=xmllog.xml

.PHONY: test

But the xmllog.xml is never created. I tried to touch the xmllog.xml before running the test. I tried to fore errors in the test, to make sure failing tests are skipped. I have already commented the failing tests. But no xmllog.xml is created. Does someone have a clue?

I am running mocha-casperjs version 1.1.0-beta3.

Thanks!

Answer:

Thanks to @vanadium23 I was able to solve this way too easy issue. I was confusing the documentation of CasperJS itself and mocha-casperjs. His answer was:

In documentation there is no such option as --xunit. Instead of this, you need to use option --file=xmllog.xml

Thanks @vanadium23

Javascript to render charts not working with .getJSON

Correction: /devicedata to /products - that was a typo..

I have built a node-express app which I am using to to plot a graph from the data retrieved from mongo database. I have tried two external libraries Chart.js and Canvas.js. Both work perfectly fine when the data is hard-coded in the javascript. The moment I use $.getJSON to retrieve the data from database it stops working. On the server side code is as below:

app.get('/products', function(req, res) {
    var db = req.db;
    db.collection('products').find().toArray(function (err, items) {
       res.json(items);
    });
});

On the client side code is as below:

<script type="text/javascript">
$(document).ready(function () {

    $.getJSON("/products", function (result) {

        var chart = new CanvasJS.Chart("chartContainer", {
            title:{
                text: "Temperatures recorded this year"
                },
            data: [
                {
                    type: "column",
                    dataPoints: result,
                }
            ]
        });

        chart.render();
    });
});
</script>
<div id="chartContainer" style="height: 300px; width: 100%;">
</div>

Is there an alternative to .getJSON to retrieve data from database (mongo in this case) ? The chart is rendering as a blank canvas

How to trigger error on fs.rename command if file already exists?

I have the following code that moves files from one directory to another:

var fs = require('fs'),
    oldPath = 'firstfile.txt',
    newPath = 'temp/firstfile.txt';

fs.rename(oldPath, newPath, function (err) {
    console.log('rename callback ', err); 
});

Is it possible to trigger error if newPath file already exists?

Potly on Nodejs - "socket hang up and read ECONNRESET"

I have some error when use Plotly on Nodejs, can you help me?

I write a demo to generate image from splotly api, every 20 seconds, server will create a chart image. Some images is good, but then throw err as image:

enter image description here

enter image description here

My code:

setInterval(function() {
  plotly.getImage(figure, imgOpts, function(error, imageStream) {
    if (error) return console.log(error);
    var time = new Date();
    console.log("1--" + new Date());
    var fileStream = fs.createWriteStream(time + '.png');
    console.log("2--" + new Date());
    imageStream.pipe(fileStream);

    //            setTimeout(function(){
    //                console.log("3--"+new Date());
    //                //createPdfkit(time);
    //            },10000);


    //            imageStream.on('end',function(){
    //
    //            });
  });

}, 20000);