问题
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 .
回答1:
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.
来源:https://stackoverflow.com/questions/57261932/can-not-created-the-datasource-with-mysql