router.post('/api/products', async (req, res) => { console.log(`The product is ${JSON.stringify(req.body)}`) let productToAdd = req.body; productToAdd = {name:'iPhone', numOfSales:5} if(!validateProduct(productToAdd)) throw new Error('Invalid product') const DBResult = await knex.insert(productToAdd).into("products"); res.json(req.body).end(); });
router.post('/', function(req, res) { db.insert(req.body).returning('*').into('todo').then(function(data) { res.send(data); }) console.log(req.body); // INSERT INTO table_name(column1, column2) VALUES(value_1, value_2) // SELECT * FROM table WHERE id = inserted_row });
async tableInsert ({ table, rows, truncate }) { const client = await knex({ client: 'pg', connection: config.env.database }); if (truncate) { await client.raw(`TRUNCATE ${table} RESTART IDENTITY CASCADE`); } return client.insert(rows, ['id']).into(table); }
router.post('/newUser', (req,res) => { bcrypt.genSalt(async (err,salt) => { const insertData = req.body; insertData.password = await bcrypt.hash(insertData.password, salt) db.insert(insertData).into('users').returning('*').then( data =>{ res.send({salt: salt, data: data}) }).catch(err => { console.log(err) res.send(err) }) }) })
router.post('/register', (req,res) =>{ bcrypt.genSalt(async (err,salt) => { const insertData = { username: req.body.username, password: await bcrypt.hash(req.body.password, salt) } // INSERT INTO tablename(column1,column2) VALUES(columns1_value,columns2_value); db.insert(insertData).into('password') .returning('*').then(data => { //RETURN SELECT * FROM table WHERE id = inserted_row; res.send({salt: salt, data: data}) }).catch(err => { console.log(err) res.send(err) }) }) })
.into("products") .then(id => { console.log(id);
router.post('/', (req,res) => { db.insert(req.body).into('todo').returning('*').then(data =>{ res.send(data) }) })