问题描述
我正试图让Loopback发现并建立我的第一个表.我在下面底部的页面上使用了简单的示例:
I'm trying to get Loopback to discover and build my first table. I've used the simple example on their page at the bottom here:
http://docs.strongloop.com/display/LB/Database+discovery + API#DatabasediscoveryAPI-通过发现构建模型的示例
,我看到了我发现的表的输出,但是API Explorer没有显示表或任何新生成的端点.另外,不会使用新的表对象更新model-config.js文件.这是在服务器启动时完成的代码的基本部分:
and I see the output of the table I'm discovering, but the API Explorer doesn't show the table or any newly generated endpoints. Also, the model-config.js file is not updated with the new table object. Here is the basic section of the code done on server start:
var loopback = require('loopback');
var boot = require('loopback-boot');
var DataSource = require('loopback-datasource-juggler').DataSource;
var mysqlSource = require('./datasources.json');
var dataSource = new DataSource('mssql', mysqlSource.mysqlserver);
var app = module.exports = loopback();
// Set up the /favicon.ico
app.use(loopback.favicon());
// request pre-processing middleware
app.use(loopback.compress());
// -- Add your pre-processing middleware here --
dataSource.discoverAndBuildModels('CATS', {owner: 'mamacat'}, function (err, models) {
models.Cat.find(function (err, cat) {
if (err) {
console.error(err);
} else {
console.log(cat);
}
dataSource.disconnect();
});
});
// boot scripts mount components like REST API
boot(app, __dirname);
总而言之,这可以正常运行,没有错误.但是http://localhost:3000/explorer
To summarize, this runs, no errors. But no new models show on http://localhost:3000/explorer
推荐答案
似乎发现脚本仅显示输出,而不创建模型文件.我在回送文档中找到了一些说明:
Seems that discovery scripts only shows the output and doesn't create the model files. I found some instructions on loopback docs:
http://docs.strongloop.com/display/public/LB/Discovering + models + from + relational + databases
在基本过程部分中,第二步:
2.使用fs.writeFile()将输出保存在common/models/model-name.json中.
2. Use fs.writeFile() to save the output in common/models/model-name.json.
因此,您可以尝试以下方法:
So you can try the following approach:
- 在 yourloopbackproject/server/datasources.json 文件中设置mysql数据:
- Setup your mysql data in yourloopbackproject/server/datasources.json file:
{
"db": {
"name": "db",
"connector": "memory"
},
"accountDs": {
"host": "mysqlServerName",
"port": 3306,
"database": "databaseName",
"username": "username",
"password": "password!",
"name": "accountDs",
"connector": "mysql"
}
}
-
创建模型文件夹(如果不存在): yourloopbackproject/common/models .
在 yourloopbackproject/server/bin 文件夹上创建 discovery-and-build.js 脚本:
var path = require('path');
var fs = require('fs');
var app = require(path.resolve(__dirname, '../server'));
var outputPath = path.resolve(__dirname, '../../common/models');
var dataSource = app.dataSources.accountDs;
function schemaCB(err, schema) {
if(schema) {
console.log("Auto discovery success: " + schema.name);
var outputName = outputPath + '/' +schema.name + '.json';
fs.writeFile(outputName, JSON.stringify(schema, null, 2), function(err) {
if(err) {
console.log(err);
} else {
console.log("JSON saved to " + outputName);
}
});
}
if(err) {
console.error(err);
return;
}
return;
};
dataSource.discoverSchema('tableName',{schema:'schemaName'},schemaCB);
此脚本基于: http://www.reddit.com /r/strongloop/comments/2upy76/autodiscoveryjs_recipe/
-
执行脚本后,您将在models文件夹中找到一个.json文件.转到基本过程部分的第3步: http://docs.strongloop.com/display/public /LB/从+关系+数据库中发现+模型+
按照以下步骤在REST上公开您的模型: http://docs.strongloop.com/display/public/LB /Exposed + models + over + REST
Follow these steps to expose your model over REST:http://docs.strongloop.com/display/public/LB/Exposing+models+over+REST
我希望这会有所帮助!
这篇关于环回discoverAndBuildModels不生成模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!