javascript - node-js: expree-formidable parse value - array overwritten -


i have problem parsing forms in node js , express-formidable.

i figured out problem occurs don't know why , how fix correctly.

my problem when want add people array , save them in .json file can add first person --> ok

if want add second,third,... second person added, existing person in array(and json-file) overwritten second person. if add 1. howard stored in array. if add 2. michael array has 2 entries michael , no howard.

the problem is:

app.post('/profiles', formidable()); 

because if delete line , re-write storeprofil function normal formidableparse works , can add people array:

storeprofile: function (req) {       var form = new formidable.incomingform();     form.keepextensions = true;     form.parse(req, (err, fields, files) => {            //var err ="";         app.fixtures.push(fields);            ............. }); 

i have mvc pattern - can find code below:

could please tell me why not work express-formidable?

thanks!

entry.js

const express = require('express');  const app = express(); require('./routing')(app);  app.listen(3000); 

routing.js

const formidable = require('express-formidable'),     homecontroller = require('./controllers/home.controller'),     peoplecontroller = require('./controllers/people.controller.js');  module.exports = function (app) {     app.get('/', homecontroller.gethome);     app.get('/new', peoplecontroller.getnewprofileform);        //this causes problem!!!!      app.post('/profiles', formidable());      app.post('/profiles', peoplecontroller.postnewprofile); }; 

people.controller.js

const util = require('util'),     people = require('./../models/people.model.js'),     profileviews = require('./../views/people.views.js');       function handlenewprofileformget (req, res) {     res.send(profileviews.newprofileview()); }      function handlenewprofilepost (req, res) {      people.storeprofile(req.fields);       res.status(201).send(message);     res.end(); }  module.exports = {     getnewprofileform: handlenewprofileformget,     postnewprofile: handlenewprofilepost }; 

people.model.js

 const util = require('util');      const fs = require('fs');      const formidable = require('formidable');  const app = {         peop: []     };  const people = {     storeprofile: function (profile) {          var err ="";              app.peop.push(profile);                  console.log("app " + util.inspect(app.peop));          if (fs.existssync('./people/people.json'))         {             fs.unlinksync('./people/people.json');                  if (err) throw err;              //  res.end();         }          if (!fs.existssync('./people/')) {        fs.mkdirsync('./people/');    }             fs.writefile('./people/people.json', json.stringify(app.peop), 'utf8', (err) => {                if (err) throw err;               // res.end();         }); });       };  module.exports = people; 

people.views.js

const hbs = require('handlebars'); const layout = require('./layout');  hbs.registerpartial('profile-form',     `<h1>ne</h1>      <form action="/profiles" method="post">          <p><label>k: <br/><input type="text" name="nickname"></label></p>               <p><label>v: <br/><input type="text" name="firstname"></label></p>               <p><label>n: <br/><input type="text" name="lastname"></label></p>                  <p><button type="submit">create</button></p>           </form>      `);  function rendernewprofileview () {     const viewmodel = { bodypartial: 'profile-form' };     return layout(viewmodel); }   module.exports = {     newprofileview: rendernewprofileview }; 


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -