Search code examples
javascriptnode.jsmongodbstrapi

Strapi uploaded files are not displaying on website


I have many files which are stored in upload_file collection in mongodb and they have relations with related content types. However when I open Strapi CMS UI, I cannot see the file attached on its content type.

I am using Strapi v3.4.6 — Community Edition.

In the first picture is showing the my one of upload_file collection item. Its relation is shown in red circle.

In the second picture is showing the my main content type collection item. You see that its id and upload_file rel id is matching.

But in Strapi UI, this file is not linked to model. The file exists in file system of Strapi. However it is not visible

Here is showing the my one of upload_file collection item. Its relation is shown in red circle

Here is showing the my main content type collection item. You see that its id and upload_file rel id is matching

But in Strapi UI, this file is not linked to model. The file exists in file system of Strapi. However it is not visible

I can add this file manually, but is there any quick way to do this?


Solution

  • You need to migrate the database. We solved with a basic script.

    Run http://localhost:3000/migrate

    var MongoClient = require('mongodb').MongoClient;
    var url = "mongodb://localhost:27017";
    var dbName = "YOURDBNAME";
    const express = require('express')
    const app = express()
    const port = 3000
    var _db;
    var _dbo;
    
    var tables = {
        "table1": "TabLE1",
        "table2": "TABle2",
    }
    
    app.get('/migrate', (req, res) => {
      res.send('Started!')
      
      _dbo.collection("upload_file").find({}).toArray(function(err, result) {
        if (err) throw err;
        result.forEach(function (item) {
          if (item.related.length > 0) {
            var related = item.related[0];
            var query = { '_id': related.ref };
            var newvalues = { $set: {} };
            newvalues.$set[related.field] = item._id;
            var tableName = related.kind.toLowerCase();
            _dbo.collection(tables[tableName]).updateOne(query, newvalues, function(err, res) {
              if (err) throw err;
              console.log(res != null ? res.ops : null);
            });
          }
        })
        // db.close();
      });
    })
    
    
    MongoClient.connect(url, function(err, db) {
      if (err) throw err;
      _dbo = db.db(dbName);
    });
    
    app.listen(port, () => {
      console.log(`Example app listening at http://localhost:${port}`)
    })
    
    function capitalizeFirstLetter(string) {
      return string.charAt(0).toUpperCase() + string.slice(1);
    }
    
    // Run http://localhost:3000/migrate