I am creating loopback applications with mysql . I set out the datasource will be mysql but when i run the applications i got following error in console windows .
ReferenceError: Cannot create data source "shop": Cannot initialize connector "mysql": time is not defined
Here is my datasource.json code .
{
"db": {
"name": "db",
"connector": "memory"
},
"shop": {
"host": "localhost",
"port": 8081,
"url": "",
"database": "shoppingdatabase",
"password": "",
"name": "shop",
"user": "root",
"connector": "mysql",
"connectTimeout":9000000
}
}
Here is the code for model.json
{
"_meta": {
"sources": [
"loopback/common/models",
"loopback/server/models",
"../common/models",
"./models"
],
"mixins": [
"loopback/common/mixins",
"loopback/server/mixins",
"../common/mixins",
"./mixins"
]
},
"User": {
"dataSource": "db"
},
"AccessToken": {
"dataSource": "db",
"public": false
},
"ACL": {
"dataSource": "db",
"public": false
},
"RoleMapping": {
"dataSource": "db",
"public": false,
"options": {
"strictObjectIDCoercion": true
}
},
"Role": {
"dataSource": "db",
"public": false
},
"carts": {
"dataSource": "shop",
"public": true
},
"products": {
"dataSource": "shop",
"public": true
},
"users": {
"dataSource": "shop",
"public": true
},
"vendors": {
"dataSource": "shop",
"public": true
}
}
Here is the screen shot when i run the server .
Change your datasource.json as :
{
"db": {
"host": "localhost",
"port": 3306,
"url": "",
"database": "shoppingdatabase",
"password": "",
"name": "db",
"user": "root",
"connector": "mysql",
"connectTimeout":9000000
}
}
and model.json as
{
"_meta": {
"sources": [
"loopback/common/models",
"loopback/server/models",
"../common/models",
"./models"
],
"mixins": [
"loopback/common/mixins",
"loopback/server/mixins",
"../common/mixins",
"./mixins"
]
},
"User": {
"dataSource": "db"
},
"AccessToken": {
"dataSource": "db",
"public": false
},
"ACL": {
"dataSource": "db",
"public": false
},
"RoleMapping": {
"dataSource": "db",
"public": false,
"options": {
"strictObjectIDCoercion": true
}
},
"Role": {
"dataSource": "db",
"public": false
},
"carts": {
"dataSource": "db",
"public": true
},
"products": {
"dataSource": "db",
"public": true
},
"users": {
"dataSource": "db",
"public": true
},
"vendors": {
"dataSource": "db",
"public": true
}
}
and before that create a database in MySQL manually with the name "shoppingdatabase" as loopback will need the database pre-created.
And keep it password protected too.
Related
Because public IPFS gateway is too slow, I set up own ipfs cluster using kubernetes on AWS.
However, when I tried to get files from the cluster, I succeeded for some files but failed for others consistently(failed one kept failing).
How do I debug this? Did I make mistake on configuration? Here's the configuration I used.
{
"API": {
"HTTPHeaders": {
"Access-Control-Allow-Methods": [
"PUT",
"POST"
],
"Access-Control-Allow-Origin": [
"http://localhost:3000",
"http://127.0.0.1:5001",
"https://webui.ipfs.io"
]
}
},
"Addresses": {
"API": "/ip4/0.0.0.0/tcp/5001",
"Announce": [],
"AppendAnnounce": [],
"Gateway": "/ip4/0.0.0.0/tcp/8080",
"NoAnnounce": [],
"Swarm": [
"/ip4/0.0.0.0/tcp/4001",
"/ip6/::/tcp/4001",
"/ip4/0.0.0.0/udp/4001/quic",
"/ip6/::/udp/4001/quic"
]
},
"AutoNAT": {},
"Bootstrap": [
"/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
"/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
"/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb",
"/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt",
"/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
"/ip4/104.131.131.82/udp/4001/quic/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ"
],
"DNS": {
"Resolvers": {}
},
"Datastore": {
"BloomFilterSize": 0,
"GCPeriod": "1h",
"HashOnRead": false,
"Spec": {
"mounts": [
{
"child": {
"path": "blocks",
"shardFunc": "/repo/flatfs/shard/v1/next-to-last/2",
"sync": true,
"type": "flatfs"
},
"mountpoint": "/blocks",
"prefix": "flatfs.datastore",
"type": "measure"
},
{
"child": {
"compression": "none",
"path": "datastore",
"type": "levelds"
},
"mountpoint": "/",
"prefix": "leveldb.datastore",
"type": "measure"
}
],
"type": "mount"
},
"StorageGCWatermark": 90,
"StorageMax": "10GB"
},
"Discovery": {
"MDNS": {
"Enabled": true,
"Interval": 10
}
},
"Experimental": {
"AcceleratedDHTClient": false,
"FilestoreEnabled": false,
"GraphsyncEnabled": false,
"Libp2pStreamMounting": false,
"P2pHttpProxy": false,
"StrategicProviding": false,
"UrlstoreEnabled": false
},
"Gateway": {
"APICommands": [],
"HTTPHeaders": {
"Access-Control-Allow-Headers": [
"X-Requested-With",
"Range",
"User-Agent"
],
"Access-Control-Allow-Methods": [
"GET"
],
"Access-Control-Allow-Origin": [
"*"
]
},
"NoDNSLink": false,
"NoFetch": false,
"PathPrefixes": [],
"PublicGateways": null,
"RootRedirect": "",
"Writable": false
},
"Identity": {
"PeerID": "<intentionally hide>"
},
"Internal": {},
"Ipns": {
"RecordLifetime": "",
"RepublishPeriod": "",
"ResolveCacheSize": 128
},
"Migration": {
"DownloadSources": [],
"Keep": ""
},
"Mounts": {
"FuseAllowOther": false,
"IPFS": "/ipfs",
"IPNS": "/ipns"
},
"Peering": {
"Peers": null
},
"Pinning": {
"RemoteServices": {}
},
"Plugins": {
"Plugins": null
},
"Provider": {
"Strategy": ""
},
"Pubsub": {
"DisableSigning": false,
"Router": ""
},
"Reprovider": {
"Interval": "12h",
"Strategy": "all"
},
"Routing": {
"Type": "dht"
},
"Swarm": {
"AddrFilters": null,
"ConnMgr": {
"GracePeriod": "20s",
"HighWater": 900,
"LowWater": 600,
"Type": "basic"
},
"DisableBandwidthMetrics": false,
"DisableNatPortMap": false,
"RelayClient": {
"Enabled": true
},
"RelayService": {
"Enabled": true
},
"Transports": {
"Multiplexers": {},
"Network": {},
"Security": {}
}
}
}
userauth.json
{
"name": "userauth",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"id":{
"type":"number",
"required":true,
"length":11,
"mysql":
{
"columnName":"id",
"dataType":"INT",
"dataLength":11,
"nullable":"N"
}
},
"firstname":{
"type":"string",
"required":true,
"length":25,
"mysql":
{
"columnName":"firstname",
"dataType":"VARCHAR",
"dataLength":25,
"nullable":"N"
}
},
"lastname":{
"type":"string",
"required":true,
"length":25,
"mysql":
{
"columnName":"lastname",
"dataType":"VARCHAR",
"dataLength":25,
"nullable":"N"
}
},
"email":{
"type":"string",
"required":true,
"length":50,
"mysql":
{
"columnName":"email",
"dataType":"VARCHAR",
"dataLength":50,
"nullable":"N"
}
},
"password":{
"type":"string",
"required":true,
"length":30,
"mysql":
{
"columnName":"password",
"dataType":"VARCHAR",
"dataLength":30,
"nullable":"N"
}
},
"dd":{
"type":"number",
"required":true,
"length":2,
"mysql":
{
"columnName":"dd",
"dataType":"INT",
"dataLength":2,
"nullable":"N"
}
},
"mm":{
"type":"number",
"required":true,
"length":2,
"mysql":
{
"columnName":"mm",
"dataType":"INT",
"dataLength":2,
"nullable":"N"
}
},
"yyyy":{
"type":"number",
"required":true,
"length":4,
"mysql":
{
"columnName":"yyyy",
"dataType":"INT",
"dataLength":4,
"nullable":"N"
}
}
},
"validations": [],
"relations": {},
"acl": [],
"methods": {}
}
userauth.js
'use strict';
module.exports = function(userauth) {
};
model-config.json
{
"_meta": {
"sources": [
"loopback/common/models",
"loopback/server/models",
"../common/models",
"./models"
],
"mixins": [
"loopback/common/mixins",
"loopback/server/mixins",
"../common/mixins",
"./mixins"
]
},
"User": {
"dataSource": "db"
},
"AccessToken": {
"dataSource": "db",
"public": false
},
"ACL": {
"dataSource": "db",
"public": false
},
"RoleMapping": {
"dataSource": "db",
"public": false
},
"Role": {
"dataSource": "db",
"public": false
},
"userauth": {
"dataSource": "db",
"public": true
}
}
datasource.json
{
"db": {
"host": "localhost",
"port": 3306,
"url": "",
"database": "users",
"password": "12121212",
"name": "db",
"user": "root",
"connector": "mysql"
}
}
ERROR IN RESPONSE WHEN TRYING TO GET or POST
> {
> "error": {
> "statusCode": 500,
> "name": "Error",
> "message": "ER_BAD_FIELD_ERROR: Unknown column 'model' in 'field list'",
> "code": "ER_BAD_FIELD_ERROR",
> "errno": 1054,
> "sqlMessage": "Unknown column 'model' in 'field list'",
> "sqlState": "42S22",
> "index": 0,
> "sql": "SELECT `model`,`property`,`accessType`,`permission`,`principalType`,`principalId`,`id`
> FROM `ACL` WHERE `model` IN ('userauth','*') AND `property` IN
> ('find','*') AND `accessType` IN ('READ','*') ORDER BY `id`",
mySQL db is already connected.
another point I noted that loopback is creating own db name as "acl"
and not using the db name defined while creating the model.
I have db name users, and created table acl with the exact column
names properties name in userauth.json file
LoopBack is actually not creating its own database named as "ACL". As you have created a datasource with the required details of the database, your app is using that database.
You can use the following script to identify and create tables in the database based on LoopBack models you've created.
Create a file: "createTables.js" in server folder, and add following code:
var server = require('./server');
var ds = server.dataSources.mysql;
var lbTables = ['User', 'AccessToken', 'ACL', 'RoleMapping', 'Role', 'userauth'];
ds.automigrate(lbTables, function (er) {
if (er) throw er;
console.log('Loopback tables [' - lbTables - '] created in ', ds.adapter.name);
ds.disconnect();
});
This will create all of the tables with columns based on the properties of the LoopBack models. You can run the script by moving in the server directory and running node createTable.js command.
Refer to their documentation: https://loopback.io/doc/en/lb2/Creating-database-tables-for-built-in-models.html
Warning: Auto-migration will drop an existing table if its name
matches a model name. When tables with data exist, use
auto-update to avoid data loss.
This is actually used for creating tables for built-in loopback models, but you can add other models (using their names) to lbTables array and the script will create the database tables for those models too.
I have a Loopback API in which I have a container and I'm trying to add some relations to my models (using this) but when it comes to adding relations to container.js, it does not work anymore and I get this error :
/home/mowso/Documents/loopback-examples/test/node_modules/loopback-datasource-juggler/lib/datasource.js:434
modelClass[relation.type].call(modelClass, name, params);
^
TypeError: Cannot read property 'call' of undefined
at EventEmitter.<anonymous> (/home/mowso/Documents/loopback-examples/test/node_modules/loopback-datasource-juggler/lib/datasource.js:434:36)
at EventEmitter.g (events.js:260:16)
at emitOne (events.js:77:13)
at EventEmitter.emit (events.js:169:7)
at DataSource.setupDataAccess (/home/mowso/Documents/loopback-examples/test/node_modules/loopback-datasource-juggler/lib/datasource.js:540:14)
at DataSource.attach (/home/mowso/Documents/loopback-examples/test/node_modules/loopback-datasource-juggler/lib/datasource.js:752:8)
at Function.ModelClass.attachTo (/home/mowso/Documents/loopback-examples/test/node_modules/loopback-datasource-juggler/lib/model-builder.js:336:16)
at Registry.configureModel (/home/mowso/Documents/loopback-examples/test/node_modules/loopback/lib/registry.js:238:15)
at configureModel (/home/mowso/Documents/loopback-examples/test/node_modules/loopback/lib/application.js:441:16)
at EventEmitter.app.model (/home/mowso/Documents/loopback-examples/test/node_modules/loopback/lib/application.js:133:5)
I have three models like in the example I link above, they basically just have different names. Here is my container.json :
{
"name": "Container",
"base": "PersistedModel",
"properties": {
"name": {
"type": "string"
}
},
"validations": [],
"relations": {
"user": {
"type": "belongsTo",
"model": "user",
"foreignKey": "ownerId"
}
},
"acls": [],
"methods": {}
}
and my user.json :
{
"name": "user",
"base": "User",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {},
"validations": [],
"relations": {
"containers": {
"type": "hasOne",
"model": "Container",
"foreignKey": "ownerId"
},
"companies": {
"type": "hasOne",
"model": "company",
"foreignKey": "ownerId"
}
},
"acls": [],
"methods": {}
}
Here is the model-config.json :
{
"_meta": {
"sources": [
"loopback/common/models",
"loopback/server/models",
"../common/models",
"./models"
],
"mixins": [
"loopback/common/mixins",
"loopback/server/mixins",
"../common/mixins",
"./mixins"
]
},
"User": {
"dataSource": "db"
},
"AccessToken": {
"dataSource": "db",
"public": false
},
"ACL": {
"dataSource": "db",
"public": false
},
"RoleMapping": {
"dataSource": "db",
"public": false
},
"Role": {
"dataSource": "db",
"public": false
},
"Container": {
"dataSource": "myfile",
"public": true
},
"user": {
"dataSource": "db",
"public": false
},
"company": {
"dataSource": "db",
"public": false
}
}
and finally my datasource.json, notice that I have a storage folder at the root of my project :
{
"db": {
"name": "db",
"connector": "mongodb"
},
"myfile": {
"name": "myfile",
"connector": "loopback-component-storage",
"provider": "filesystem",
"root": "storage"
}
}
Your foreign key is not correct, as per the documentation it should be,
Relation name: Camel case of the model name, for example, for the "supplier" model the relation is "supplier".
Foreign key: The relation name appended with ‘Id’, for example, for relation name "supplier" the default foreign key is "supplierId".
So in your case it should be
"foreignKey": "userId"
https://docs.strongloop.com/display/public/LB/HasOne+relations
And you should use the slc relations instead of editing the json files.
I believe Container should inherit Model, not PersistedModel. By default, a Container is a folder inside a filesystem, so there is no choice of persisting it or not.
{
"name": "Container",
"base": "Model",
I made a website with nodejs and strongloop. I used the mysql connector and tested the connection in Strongloop Arc. The connection is OK. I created the models with the arc tool (discover models). It was also OK.
But when I open the Strongloop API Explorer and do the test to get the data I Always get the error:
"ER_NO_DB_ERROR: No database selected".
Have you confirmed that your model was connected to dbAuction instead of db?
When using arc to add a model, the 'datasource' should be 'dbAuction'.
You can also check the model-config.json in 'server/' directory. A property named 'dataSource' belongs to the model name like
"modelname" : {
"dataSouce":"dbAuction"
...
},
...
This is the model-config.json
{
"_meta": {
"sources": [
"loopback/common/models",
"loopback/server/models",
"../common/models",
"./models"
],
"mixins": [
"loopback/common/mixins",
"loopback/server/mixins",
"../common/mixins",
"./mixins"
]
},
"User": {
"dataSource": "db"
},
"AccessToken": {
"dataSource": "db",
"public": false
},
"ACL": {
"dataSource": "db",
"public": false
},
"RoleMapping": {
"dataSource": "db",
"public": false
},
"Role": {
"dataSource": "db",
"public": false
},
"newModel": {
"dataSource": "db",
"public": true
},
"Biedingen": {
"dataSource": "dbAuction",
"public": true
},
"Kavel": {
"dataSource": "dbAuction",
"public": true
},
"Gebruikers": {
"dataSource": "dbAuction",
"public": true
},
"Message": {
"dataSource": "dbAuction",
"public": true
},
"Veilingen": {
"dataSource": "dbAuction",
"public": true
},
"Sourcemessage": {
"dataSource": "dbAuction",
"public": true
},
"Veilingenkavels": {
"dataSource": "dbAuction",
"public": true
},
"Veilingsoorten": {
"dataSource": "dbAuction",
"public": true
}
and the datasources.json
{
"db": {
"name": "db",
"connector": "mysql"
},
"dbAuction": {
"host": "localhost",
"port": 3306,
"database": "auctions",
"password": "",
"name": "dbAuction",
"connector": "mysql",
"user": "root"
}
}
I can't add a comment since my reputation is too low..
I suggest you can change the 'mysql' in
"db": {
"name": "db",
"connector": "**mysql**"
into "memory" as it was initiated because the "db" is useful for some models while you changed wrongly so that 'db' cannot work well, which may lead to this problem.
I am very new to loopback. I am making my first app with it and I change my datasource to MySql. my person model look like this
person.json
{
"name": "Person",
"plural": "People",
"base":"User",
"properties": {
"firstName": {
"type": "string",
"required": true
},
"lastName": {
"type": "string",
"required": true
},
"username": {
"type": "string",
"required": true
},
"email": {
"type": "string",
"required": true
},
"password": {
"type": "string",
"required": true
}
},
"validations": [],
"relations": {},
"acls": [
{
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "ALLOW",
"accessType": "*"
}
],
"methods": []
}
and
datasource.json
{
"db": {
"name": "db",
"connector": "memory"
},
"mysql": {
"host": "localhost",
"port": 3306,
"database": "demo",
"username": "demouser",
"password": "****",
"name": "mysql",
"connector": "mysql"
}
}
and
model-config.json
{
"_meta": {
"sources": [
"../common/models",
"./models"
]
},
"AccessToken": {
"dataSource": "db",
"public": false
},
"ACL": {
"dataSource": "db",
"public": false
},
"RoleMapping": {
"dataSource": "db",
"public": false
},
"Role": {
"dataSource": "db",
"public": false
},
"Person": {
"dataSource": "mysql",
"public": true
}
}
After this when I want to create any person I cant create any person as I am unauthorized. I inserted an entry in mysql and tried to login but no luck.But when I remove User as base of person then all goes fine. But I lose all auth features inherit from User. How can I extend User using mysql as datasource properly so that I can use Person as my Users?
Thanks in Advance
Can you try to add the following ACL?
"acls": [
{
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "ALLOW",
"accessType": "*",
"property": "create"
}
],
You can debug the application using:
DEBUG=loopback:security:* node .