INSERT INTO fails with node-mysql

Pay attention to the documentation of node-mysql: If you paid attention, you may have noticed that this escaping allows you to do neat things like this: var post = {id: 1, title: ‘Hello MySQL’}; var query = connection.query(‘INSERT INTO posts SET ?’, post, function(err, result) { // Neat! }); console.log(query.sql); // INSERT INTO posts SET … Read more

Create unique autoincrement field with mongoose [duplicate]

const ModelIncrementSchema = new Schema({ model: { type: String, required: true, index: { unique: true } }, idx: { type: Number, default: 0 } }); ModelIncrementSchema.statics.getNextId = async function(modelName, callback) { let incr = await this.findOne({ model: modelName }); if (!incr) incr = await new this({ model: modelName }).save(); incr.idx++; incr.save(); return incr.idx; }; const … Read more

body-parser – extended option (qs vs querystring)

The reason for this message is that body-parser is about to change default value for extended from true to false. Extended protocol uses qs library to parse x-www-form-urlencoded data. The main advantage of qs is that it uses very powerful serialization/deserialization algorithm, capable of serializing any json-like data structure. But web-browsers don’t normally use this … Read more

Approach to multiple MySQL queries with Node.js

One should avoid the pyramid of doom: var express = require(‘express’); var Q = require(‘Q’); var app = express(); app.get(“https://stackoverflow.com/”,function(req,res){ var mysql = require(‘mysql’); var connection = mysql.createConnection({ host : ‘localhost’, user : ‘root’, password : ” }); connection.connect(); function doQuery1(){ var defered = Q.defer(); connection.query(‘SELECT 1 AS solution’,defered.makeNodeResolver()); return defered.promise; } function doQuery2(){ var … Read more