Reputation: 5914
I am trying to append a value to one of my objects fields with an .update
method, but my current code replaces the value in the field (organization.members
) with the new value captured by the field during the post method. I was looking in the documentation for a way to append the value (comma-separated, but I didn't see anything related to appending a value to my object property.
Here is my route:
var express = require('express');
var appRoutes = express.Router();
var passport = require('passport');
var localStrategy = require('passport-local').Strategy;
var models = require('../models/db-index');
appRoutes.route('/settings')
.get(function(req, res){
models.Organization.find({
where: {
organizationId: req.user.organizationId
}, attributes: ['organizationName','admin','members']
}).then(function(organization){
res.render('pages/app/settings.hbs',{
user: req.user,
organization: organization
});
})
})
.post(function(req, res, user){
models.Organization.update({
members: req.body.addMember
},{ where: {
organizationId: req.user.organizationId
}}).then(function(){
console.log("User added to Organization");
res.redirect('/app/settings');
});
});
Here is the organization model:
module.exports = function(sequelize, DataTypes) {
var Organization = sequelize.define('organization', {
organizationId: {
type: DataTypes.INTEGER,
field: 'organization_id',
autoIncrement: true,
primaryKey: true
},
organizationName: {
type: DataTypes.STRING,
field: 'organization_name'
},
admin: DataTypes.STRING,
members: DataTypes.STRING
},{
freezeTableName: true,
classMethods: {
associate: function(db) {
Organization.hasMany(db.User, {foreignKey: 'user_id'});
},
},
});
return Organization;
}
Here is the form:
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="annotation-form">
<h2>Add User</h2>
<form action="/app/settings" method="post">
<input name="addMember" type="text" value="">
<button type="submit">Add User</button>
</form>
<h2><u>Additional Users</u></h2>
{{#each organization}}
<li>{{this.members}}</li>
{{else}}
<p>No additional users</p>
{{/each}}
</div>
</div>
</div>
db-index.js:
var Sequelize = require('sequelize');
var path = require('path');
var config = require(path.resolve(__dirname, '..', '..','./config/config.js'));
var sequelize = new Sequelize(config.database, config.username, config.password, {
host:'localhost',
port:'3306',
dialect: 'mysql'
});
sequelize.authenticate().then(function(err) {
if (!!err) {
console.log('Unable to connect to the database:', err)
} else {
console.log('Connection has been established successfully.')
}
});
var db = {}
db.Organization = sequelize.import(__dirname + "/organization");
db.User = sequelize.import(__dirname + "/user");
db.Organization.associate(db);
db.sequelize = sequelize;
db.Sequelize = Sequelize;
sequelize.sync();
module.exports = db;
Upvotes: 3
Views: 2445
Reputation: 4443
I had a similar issue, in my case I was using mySQL and using the underlying CONCAT
function.
The reason I am not using +
to concatenate string is because you might get an SQL syntax error if your string contains a new line
models.Organization.update({
members: Sequelize.fn('CONCAT', Sequelize.col("members"),req.body.addMember)
},{ where: {
organizationId: req.user.organizationId
}}).then(function(){
console.log("User added to Organization");
res.redirect('/app/settings');
});
Upvotes: 2