Convert Array to Object

ECMAScript 6 introduces the easily polyfillable Object.assign: The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object. The own length property of the array is not copied because it isn’t enumerable. Also, you can use … Read more

Mongoose: CastError: Cast to ObjectId failed for value “[object Object]” at path “_id”

Short answer: use mongoose.Types.ObjectId. Mongoose (but not mongo) can accept object Ids as strings and “cast” them properly for you, so just use: However, the caveat is if req.params.id is not a valid format for a mongo ID string, that will throw an exception which you must catch. So the main confusing thing to understand is that mongoose.SchemaTypes has stuff … Read more

module.exports vs exports in Node.js

Setting module.exports allows the database_module function to be called like a function when required. Simply setting exports wouldn’t allow the function to be exported because node exports the object module.exports references. The following code wouldn’t allow the user to call the function. module.js The following won’t work. The following will work if module.exports is set. console Basically node.js doesn’t export the object that exports currently references, but exports the properties … Read more

What is the difference between node.js and io.js?

io.js is a fork of Joyent’s Node.js What are the differences? io.js: Node-forward is basically being merged into io.js forked on the basis of community-driven development and active release cycles includes many of the original Node.js developers has the future goal of “merging back with Node.js” This repository began as a GitHub fork of joyent/node where contributions, releases, … Read more

NodeJS w/Express Error: Cannot GET /

You typically want to render templates like this: However you can also deliver static content – to do so use: Now everything in the /public directory of your project will be delivered as static content at the root of your site e.g. if you place default.htm in the public folder if will be available by visiting /default.htm Take a look … Read more

MissingSchemaError: Schema hasn’t been registered for model “User”

In my models/user.js file: And in my router/index.js, I have: which throws the error: If however, in user.js, I do (in the last line) and in index.js I do var User = require(‘../models/User’);, then everything works. But it should not, because in config/pass.js I am doing var User = mongoose.model(‘User’); and it’s working flawlessly. The … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)