Skip to content

Instantly share code, notes, and snippets.

@tdwesten
Last active August 29, 2015 14:10
Show Gist options
  • Save tdwesten/d8a3c7760b836e6d4bce to your computer and use it in GitHub Desktop.
Save tdwesten/d8a3c7760b836e6d4bce to your computer and use it in GitHub Desktop.
node request
// Ik probeer mijn betaande thermo-settings te updaten met de onderstaande variable "settings".
// Maar het lukt mij niet om de nieuwe data door te sturen..
// request
var setting = {
_id: 546fb9f13a9b3e00002a8e77,
temp_tracking: true,
last_update: Fri Nov 28 2014 08:25:32 GMT+0100 (CET),
current_schedule: [
{ _id: '5479a35fc6eacfc1e38bd786',
start_time: '1970-01-01T09:00:00.000Z',
end_time: '1970-01-01T15:00:00.000Z',
target_temp: 24,
marge: 1,
__v: 0,
days: [ '6' ],
name: 'Zaterdag lounge' }
],
last_temp: 19,
current_target_temp: 19,
furnace_status: true }
};
var options = {
url: 'http://localhost:3700/thermo-settings/' + setting_id,
method: 'PUT',
multipart: [ {
'content-type': 'application/json',
body: JSON.stringify( setting )
} ]
};
request.put( options, function( err, res ) {
if ( err ) {
// console.log( err );
} else {
// console.log( res );
}
} );
// Router
'use strict';
module.exports = function( app ) {
var users = require( '../../app/controllers/users' );
var thermoSettings = require( '../../app/controllers/thermo-settings' );
// Thermo settings Routes
app.route( '/thermo-settings' )
.get( thermoSettings.list )
.post( users.requiresLogin, thermoSettings.create );
// Thermo settings Routes
app.route( '/thermo-settings/getcurrentschedule' )
.get( thermoSettings.getCurrentSchedule );
app.route( '/thermo-settings/:thermoSettingId' )
.get( thermoSettings.read )
.put( thermoSettings.update )
.delete( users.requiresLogin, thermoSettings.hasAuthorization, thermoSettings.delete );
// Finish by binding the Thermo setting middleware
app.param( 'thermoSettingId', thermoSettings.thermoSettingByID );
};
// Controller
...
/**
* Update a Thermo setting
*/
exports.update = function( req, res ) {
console.log( req );
var thermoSetting = req.thermoSetting;
thermoSetting = _.extend( thermoSetting, req.body );
thermoSetting.save( function( err ) {
if ( err ) {
return res.status( 400 ).send( {
message: errorHandler.getErrorMessage( err )
} );
} else {
res.jsonp( thermoSetting );
}
} );
};
....
// Model
'use strict';
/**
* Module dependencies.
*/
var mongoose = require( 'mongoose' ),
Schema = mongoose.Schema;
/**
* Thermo setting Schema
*/
var ThermoSettingSchema = new Schema( {
furnace_status: {
type: Boolean,
default: false
},
current_target_temp: {
type: Number,
default: 19
},
last_temp: {
type: Number,
default: 19
},
current_schedule: {
type: Array,
default: []
},
last_update: {
type: Date,
default: Date.now
},
temp_tracking: {
type: Boolean,
default: false
},
user: {
type: Schema.ObjectId,
ref: 'User'
}
} );
mongoose.model( 'ThermoSetting', ThermoSettingSchema );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment