save uploaded image names in array using mongoose












0















I am using a 3rd party library for uploading files using Ajax and when they have uploaded I want to store the file names in mongoDB.



This is my Schema:



const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const uploadSchema = new Schema({
imgName: [{
type: String
}]
});

module.exports = mongoose.model('Upload', uploadSchema);


I have tried to perform the insert in the success callback for the upload:



    if (data.isSuccess) {
const uploaded = uploader.getFileList();
for (const images of uploaded) {
console.log(images.name);
const upload = new uploadModel({
imgName: images.name
});
upload.save();
}


This is storing each image as in individual record in the database with it's own unique ID which isn't what I want. If I for example upload 2 images, I want it to be one record in the database but should look something like this I guess:



_id: ObjectId("123uashjkalh73")
> imgName: Array
0: "image1.jpg"
1: "image2.jpg"
__v: 0









share|improve this question



























    0















    I am using a 3rd party library for uploading files using Ajax and when they have uploaded I want to store the file names in mongoDB.



    This is my Schema:



    const mongoose = require('mongoose');
    const Schema = mongoose.Schema;

    const uploadSchema = new Schema({
    imgName: [{
    type: String
    }]
    });

    module.exports = mongoose.model('Upload', uploadSchema);


    I have tried to perform the insert in the success callback for the upload:



        if (data.isSuccess) {
    const uploaded = uploader.getFileList();
    for (const images of uploaded) {
    console.log(images.name);
    const upload = new uploadModel({
    imgName: images.name
    });
    upload.save();
    }


    This is storing each image as in individual record in the database with it's own unique ID which isn't what I want. If I for example upload 2 images, I want it to be one record in the database but should look something like this I guess:



    _id: ObjectId("123uashjkalh73")
    > imgName: Array
    0: "image1.jpg"
    1: "image2.jpg"
    __v: 0









    share|improve this question

























      0












      0








      0








      I am using a 3rd party library for uploading files using Ajax and when they have uploaded I want to store the file names in mongoDB.



      This is my Schema:



      const mongoose = require('mongoose');
      const Schema = mongoose.Schema;

      const uploadSchema = new Schema({
      imgName: [{
      type: String
      }]
      });

      module.exports = mongoose.model('Upload', uploadSchema);


      I have tried to perform the insert in the success callback for the upload:



          if (data.isSuccess) {
      const uploaded = uploader.getFileList();
      for (const images of uploaded) {
      console.log(images.name);
      const upload = new uploadModel({
      imgName: images.name
      });
      upload.save();
      }


      This is storing each image as in individual record in the database with it's own unique ID which isn't what I want. If I for example upload 2 images, I want it to be one record in the database but should look something like this I guess:



      _id: ObjectId("123uashjkalh73")
      > imgName: Array
      0: "image1.jpg"
      1: "image2.jpg"
      __v: 0









      share|improve this question














      I am using a 3rd party library for uploading files using Ajax and when they have uploaded I want to store the file names in mongoDB.



      This is my Schema:



      const mongoose = require('mongoose');
      const Schema = mongoose.Schema;

      const uploadSchema = new Schema({
      imgName: [{
      type: String
      }]
      });

      module.exports = mongoose.model('Upload', uploadSchema);


      I have tried to perform the insert in the success callback for the upload:



          if (data.isSuccess) {
      const uploaded = uploader.getFileList();
      for (const images of uploaded) {
      console.log(images.name);
      const upload = new uploadModel({
      imgName: images.name
      });
      upload.save();
      }


      This is storing each image as in individual record in the database with it's own unique ID which isn't what I want. If I for example upload 2 images, I want it to be one record in the database but should look something like this I guess:



      _id: ObjectId("123uashjkalh73")
      > imgName: Array
      0: "image1.jpg"
      1: "image2.jpg"
      __v: 0






      node.js mongoose






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 2 at 18:50









      user8463989user8463989

      390111




      390111
























          1 Answer
          1






          active

          oldest

          votes


















          1














          So I think there are a few things that are off here.



          First, in your schema,



          const uploadSchema = new Schema({
          imgName: [{
          type: String
          }]
          });


          imgName is noted set to an Array of objects. If you want an array of strings, you can completely skip the object notation with type: String and replace it with [String] (https://mongoosejs.com/docs/schematypes.html#arrays). I think what you want is the following:



          const uploadSchema = new Schema({
          imgName: [String]
          });


          If you are trying to create a new model for this, I simplified the code a bit using reduce



          if (data.isSuccess) {
          const uploaded = uploader.getFileList();

          // similar to your loop but i'm just using reduce here. end result is an array of your image names
          const images = uploaded.reduce((acc, image) => [...acc, image.name], );
          const upload = new uploadModel({
          imgName: images
          });

          upload.save();
          }


          Hope this helps.






          share|improve this answer


























          • Thanks for that. It is still inserting individual records per image but I think that is the way the script callback is working. if (data.isSuccess) { seems to fire every time an image is uploaded which is why it creates in individual database record for every image.

            – user8463989
            Jan 3 at 6:16











          • @user8463989 ah gotcha, that makes sense. was not able to get that pictured in without seeing the rest of the code. Seems like you're in the right track though.

            – mralanlee
            Jan 3 at 16:54












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011643%2fsave-uploaded-image-names-in-array-using-mongoose%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          So I think there are a few things that are off here.



          First, in your schema,



          const uploadSchema = new Schema({
          imgName: [{
          type: String
          }]
          });


          imgName is noted set to an Array of objects. If you want an array of strings, you can completely skip the object notation with type: String and replace it with [String] (https://mongoosejs.com/docs/schematypes.html#arrays). I think what you want is the following:



          const uploadSchema = new Schema({
          imgName: [String]
          });


          If you are trying to create a new model for this, I simplified the code a bit using reduce



          if (data.isSuccess) {
          const uploaded = uploader.getFileList();

          // similar to your loop but i'm just using reduce here. end result is an array of your image names
          const images = uploaded.reduce((acc, image) => [...acc, image.name], );
          const upload = new uploadModel({
          imgName: images
          });

          upload.save();
          }


          Hope this helps.






          share|improve this answer


























          • Thanks for that. It is still inserting individual records per image but I think that is the way the script callback is working. if (data.isSuccess) { seems to fire every time an image is uploaded which is why it creates in individual database record for every image.

            – user8463989
            Jan 3 at 6:16











          • @user8463989 ah gotcha, that makes sense. was not able to get that pictured in without seeing the rest of the code. Seems like you're in the right track though.

            – mralanlee
            Jan 3 at 16:54
















          1














          So I think there are a few things that are off here.



          First, in your schema,



          const uploadSchema = new Schema({
          imgName: [{
          type: String
          }]
          });


          imgName is noted set to an Array of objects. If you want an array of strings, you can completely skip the object notation with type: String and replace it with [String] (https://mongoosejs.com/docs/schematypes.html#arrays). I think what you want is the following:



          const uploadSchema = new Schema({
          imgName: [String]
          });


          If you are trying to create a new model for this, I simplified the code a bit using reduce



          if (data.isSuccess) {
          const uploaded = uploader.getFileList();

          // similar to your loop but i'm just using reduce here. end result is an array of your image names
          const images = uploaded.reduce((acc, image) => [...acc, image.name], );
          const upload = new uploadModel({
          imgName: images
          });

          upload.save();
          }


          Hope this helps.






          share|improve this answer


























          • Thanks for that. It is still inserting individual records per image but I think that is the way the script callback is working. if (data.isSuccess) { seems to fire every time an image is uploaded which is why it creates in individual database record for every image.

            – user8463989
            Jan 3 at 6:16











          • @user8463989 ah gotcha, that makes sense. was not able to get that pictured in without seeing the rest of the code. Seems like you're in the right track though.

            – mralanlee
            Jan 3 at 16:54














          1












          1








          1







          So I think there are a few things that are off here.



          First, in your schema,



          const uploadSchema = new Schema({
          imgName: [{
          type: String
          }]
          });


          imgName is noted set to an Array of objects. If you want an array of strings, you can completely skip the object notation with type: String and replace it with [String] (https://mongoosejs.com/docs/schematypes.html#arrays). I think what you want is the following:



          const uploadSchema = new Schema({
          imgName: [String]
          });


          If you are trying to create a new model for this, I simplified the code a bit using reduce



          if (data.isSuccess) {
          const uploaded = uploader.getFileList();

          // similar to your loop but i'm just using reduce here. end result is an array of your image names
          const images = uploaded.reduce((acc, image) => [...acc, image.name], );
          const upload = new uploadModel({
          imgName: images
          });

          upload.save();
          }


          Hope this helps.






          share|improve this answer















          So I think there are a few things that are off here.



          First, in your schema,



          const uploadSchema = new Schema({
          imgName: [{
          type: String
          }]
          });


          imgName is noted set to an Array of objects. If you want an array of strings, you can completely skip the object notation with type: String and replace it with [String] (https://mongoosejs.com/docs/schematypes.html#arrays). I think what you want is the following:



          const uploadSchema = new Schema({
          imgName: [String]
          });


          If you are trying to create a new model for this, I simplified the code a bit using reduce



          if (data.isSuccess) {
          const uploaded = uploader.getFileList();

          // similar to your loop but i'm just using reduce here. end result is an array of your image names
          const images = uploaded.reduce((acc, image) => [...acc, image.name], );
          const upload = new uploadModel({
          imgName: images
          });

          upload.save();
          }


          Hope this helps.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 3 at 1:29

























          answered Jan 3 at 1:03









          mralanleemralanlee

          244110




          244110













          • Thanks for that. It is still inserting individual records per image but I think that is the way the script callback is working. if (data.isSuccess) { seems to fire every time an image is uploaded which is why it creates in individual database record for every image.

            – user8463989
            Jan 3 at 6:16











          • @user8463989 ah gotcha, that makes sense. was not able to get that pictured in without seeing the rest of the code. Seems like you're in the right track though.

            – mralanlee
            Jan 3 at 16:54



















          • Thanks for that. It is still inserting individual records per image but I think that is the way the script callback is working. if (data.isSuccess) { seems to fire every time an image is uploaded which is why it creates in individual database record for every image.

            – user8463989
            Jan 3 at 6:16











          • @user8463989 ah gotcha, that makes sense. was not able to get that pictured in without seeing the rest of the code. Seems like you're in the right track though.

            – mralanlee
            Jan 3 at 16:54

















          Thanks for that. It is still inserting individual records per image but I think that is the way the script callback is working. if (data.isSuccess) { seems to fire every time an image is uploaded which is why it creates in individual database record for every image.

          – user8463989
          Jan 3 at 6:16





          Thanks for that. It is still inserting individual records per image but I think that is the way the script callback is working. if (data.isSuccess) { seems to fire every time an image is uploaded which is why it creates in individual database record for every image.

          – user8463989
          Jan 3 at 6:16













          @user8463989 ah gotcha, that makes sense. was not able to get that pictured in without seeing the rest of the code. Seems like you're in the right track though.

          – mralanlee
          Jan 3 at 16:54





          @user8463989 ah gotcha, that makes sense. was not able to get that pictured in without seeing the rest of the code. Seems like you're in the right track though.

          – mralanlee
          Jan 3 at 16:54




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011643%2fsave-uploaded-image-names-in-array-using-mongoose%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith