Как создать новую запись через JSON Data - PullRequest
0 голосов
/ 04 августа 2020

Здесь у меня возникают некоторые трудности с тем, как создавать новую запись всякий раз, когда я создаю один файл csv и загружаю его, а теперь, как я могу создать новую запись через это, пожалуйста, помогите мне.!

Это мой файл CSV.

name, companyId, engineId, colorId

car1,123,123,123

car2,456,456,456

uploadAvatar: function (req, res) {

    let fileInputName = 'assets/excel/carData.csv';
    let fileOutputName = 'myOutputFile.json';

    req.file('excel').upload({
      // don't allow the total upload size to exceed ~10MB
      dirname: require('path').resolve(sails.config.appPath, './assets/excel'),
      saveAs: 'carData.csv',
      maxBytes: 10000000
    }, function whenDone(err, uploadedFiles) {
      if (err) {
        return res.serverError(err);
      }
      // If no files were uploaded, respond with an error.
      if (uploadedFiles.length === 0) {
        return res.badRequest('No file was uploaded');
      }


      csvToJson.generateJsonFileFromCsv(fileInputName, fileOutputName);
      csvToJson.fieldDelimiter(',') .getJsonFromCsv(fileInputName);
      let json = csvToJson.getJsonFromCsv("assets/excel/carData.csv");
      csvToJson.formatValueByType().getJsonFromCsv(fileInputName);

      //var result = [];
      var name = "";
      var comapanyId = "";
      var engineId = "";
      var colorId = "";
      
      for (let i = 0; i < json.length; i++) {
        console.log(json[i]);
        **// How to create new record ?**
      }

      return res.json({
        message: uploadedFiles.length + ' file(s) uploaded successfully!',
      });

    });

  },

Это файл моей модели

module.exports = {
  tableName: 'Car',
  schema: true,
  attributes: {
    name: {
      type: 'STRING'
    },
    companyId: {
      model: 'Master',
      columnName: 'companyId'
    },
    engineId: {
      model: 'Master',
      columnName: 'engineId'
    },
    colorId: {
      model: 'Master',
      columnName: 'colorId'
    },
    car: {
      collection: 'Order',
      via: 'carId'
    },
  },

};

1 Ответ

0 голосов
/ 04 августа 2020

Вы должны вызвать метод .createEach ([cars]).

...