3

I'm using loopback storage component "loopback-component-storage" to upload files. But problem is after adding this storage component and a Model for it named as Container, I'm not able to migrate models in my application to database.

Following is error what i get

Error: Cannot create data source "storage": Cannot initialize connector "loopback-component-storage": FileSystemProvider: Path does not exist: ./server/files
    at new FileSystemProvider

But I've /server/files directory as well in project described through this snapshot

Following is datasources.json file

{
  "school": {
    "host": "127.0.0.1",
    "port": 3306,
    "url": "",
    "database": "school_db",
    "password": "root",
    "name": "school",
    "user": "root",
    "connector": "mysql"
  },
  "storage": {
    "name": "storage",
    "connector": "loopback-component-storage",
    "provider": "filesystem",
    "root": "./server/files"
  }
}

Following is model-config.json (partially, not full)

{
  "_meta": {
    "sources": [
      "loopback/common/models",
      "loopback/server/models",
      "../common/models",
      "./models"
    ],
    "mixins": [
      "loopback/common/mixins",
      "loopback/server/mixins",
      "../common/mixins",
      "./mixins"
    ]
  },
  "Container": {
    "dataSource": "storage",
    "public": true
  }
}

Following is Container (to upload/download files)

{
  "name": "Container",
  "plural": "containers",
  "base": "Model",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {},
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": {}
}

And following is my script to update model, say Address

var server = require('./../../server');
var ds = server.dataSources.school;
var tables = ['Address'];
ds.autoupdate(tables, function(er,result) {
  if (er) throw er;

  ds.discoverModelProperties('Address', function (err, props) {
    console.log(props);
  });

  ds.disconnect();
});

Can someone please help me in figuring out the problem, why storage component is throwing exception of that files directory doesn't exist

veenaMSL
  • 101
  • 1
  • 9
Fakhar Zaman
  • 71
  • 2
  • 5
  • Try to run your application as root, e.g. `sudo npm start`. Or make sure that your application has permissions to work with your storage folder. – Serg Jan 08 '18 at 21:14
  • Maybe this link helps you https://stackoverflow.com/questions/47732182/where-to-place-images-and-other-components-folder-in-loopback/ – rastemoh Jan 09 '18 at 06:33
  • you can look at this repo i created to make use of the same and it works but locally https://github.com/rahulrsingh09/loopback-Angular-Starter – Rahul Singh Jan 12 '18 at 08:19

4 Answers4

1

Try adding this in server.js

var ds = loopback.createDataSource({
    connector: require('loopback-component-storage'),
    provider: 'filesystem',
    root: 'server/files'
});

var storage = ds.createModel('storage');
app.model(storage);

check the documentation here too

Tom
  • 118
  • 1
  • 12
0

I think issue in root parameter,

"storage": {
      "name": "storage",
      "connector": "loopback-component-storage",
      "provider": "filesystem",
      "root": "./files" 
    }

Please refer : Link

https://strongloop.com/strongblog/working-with-file-storage-and-loopback/

Community
  • 1
  • 1
IftekharDani
  • 3,619
  • 1
  • 16
  • 21
0

Do you have a files folder created inside the server folder?

If no? you have to create a folder named "files"

If yes? then your path declaration might be wrong. Try using full path like this

"root": "/home/ubuntu/Documents/project/server/files"
0

Just add another dot in front of your root directory.

"root": "../server/files"

The problem is your updation or migration script cannot find the directory. For me the migration script was in a bin directory and this directory structure was one level above.

Once the migration is done revert back.

Asad Khan
  • 11,469
  • 13
  • 44
  • 59