“Sell me this pen” – a developer’s take

Recently I came across an interesting question on Quora that is “What are best responses to ‘sell me this pen?’ question?“. After reading various answers from experts following is my interpretation: One should start by asking questions to the customer and slowly raising the need to buy a pen. Once you have these in place then next step would be to promote your pen and finally sell it. As soon as I got this summary in my mind, I started thinking to link same in software development world and following are my thoughts for same.

questions-1328466_640For the first point, that is about asking questions and raising the need for pen, as far as the raising need part is concerned, that is something client will take care as he/she wants to launch his product, that we are developing, in the market and increase awareness and need for same so everyone will use it and eventually buy it if it’s paid software. For the questioning part, I have been with developers that falls on both side of the equation that is one who asks bunch of questions to clarify things before starting implementation and one who directly jumps on coding without asking anything and raise doubts as they come which sometimes lead to a system which completely different from what customer envisioned.

Chiefly, new developers are falling in the later technique and one the reason that I believe could be the new Agile way of development. Because in Agile way, our development should be open to changes at any point in time, even late. One of the major drawbacks for such technique is new requirements that emerge as a result of some clarification and affecting scope or delivery of the product.

digital-1320699_640Continuing on the sell the pen thoughts, next step that I interpreted is to promote and sell. Again this mainly falls under client’s coat, but still there are some areas where developers and mostly designers can help. For instance, designing a brochure for a product that captures everyone eyes when distributed or doing SEO of web application to make sure it ends up in a higher position in search results. There is also a thing called supporting a software and I have seen freelancers and some companies as well who goes beyond expectations in this area and delight their customer by contributing to their success. After all, if the client is successful then that adds up to your success as well.

To conclude, I believe that in a completely different dimension if a software developer is being asked “sell me this pen” question than there are high chances that he/she will nail it. Let me know your thoughts or your interpretations and linking of “sell me this pen” question to IT industry.

Mongoose gotchas

I am using Mongoose since last few months and while querying MongoDB in Node.js and getting results. While doing that I came up with some cases that made me scratch my head for more than couple hours just to figure our it is little ‘gotcha’ (may be because I did not read through all the docs of Mongoose).

Listing my cases below so that if anyone ever comes to same mistake and tries to find for solution online, this can be helpful.

I have tried all following scenarios in 4.0.6 version of Mongoose with MongoDB 3.0.4.

1. When you query using a model and in result you see bunch of fields but when you try to access them you get undefined.

Ex.: (assuming you have user collections with name and email field in it.)

var UserSchema = new Schema({
 name: String
});

var User = mongoose.model('User', UserSchema);
User.find({}, function(err, users) {
 users.forEach(function(user) {
 console.log(user); // this will print something like {'name':'harsh', 'email': 'abc@abc.com'}
 console.log(user.email); // this will print 'undefined'
 });
});

Solution:
You need to add missing field entries in Schema definition, mongoose only binds fields that are in schema definition while creating objects.
Change above schema to following:


var UserSchema = new Schema({
 name: String,
 email: String
});

And now when you run same example above it should print email id of that particular record.

2. Auto conversion of field values in ‘find’ and ‘aggregate’ query.

Ex.:


var UserSchema = new Schema({
 name: String,
 cTime: Number
});

var User = mongoose.model('User', UserSchema);
User.find({
 cTime: '123456' // here we are passing value as string but it works
 // some how mongoose converts it before query may be
}, function(err, users) {
 console.log(users); // print users that match the cTime value
});

Now if you try same thing with aggregate function then it will not work:


User.aggregate({
 $match: { cTime : '1427807167' } // mongoose does not convert it to number this time
}, function(err, users) {
 console.log(users); // this will be empty, no match
});

In case of aggregate you have to take care of conversion by your own. Most likely case for this is getting time or other numeric values in req.query and then directly using it in $match query of aggregate. Instead you should first convert it to specific type of field and then use that in query.


User.aggregate({
 $match: { cTime : parseInt(req.query.time) } // assuming req.query.time has the value for cTime
}, function(err, users) {
 console.log(users); // now this will be not empty and prints the matching records.
});

3. Updating array items and calling save

When you just update an item in array of objects and call save on it, it will update the array in database. To make it work you need to mark the array field as modified.
user.markModified(‘array’); // this will mark array field of user object
if you call user.save after this then it will update array in database.

More info on this: http://stackoverflow.com/questions/17865081/updating-array-within-mongodb-record-with-mongoose

4. Listen to db connection drop event

This is probably not a gotcha, but it really made me work about an hour before knowing the solution. When connection to database is dropped for some reason then you will get it as ‘error’ event and not in ‘disconnected’ event. I tried to listed to ‘disconnected’ event and do reconnect but found that it does not even call it when db connection is crashed for some reason. It listens on ‘error’ event in that case so if you are planning to catch event for db connection failure and do operation based on it.

That’s all for now.

If you have seen any more while working with it, do let all know by commenting on the post. I will also update it with more as I come to know.

%d bloggers like this: