在环回中感到困惑
got confused in loopback
大家好,我是 loopback 的新手,请问有人能帮我解决这个问题吗?
1. 我知道如何从环回服务器呈现 html 页面,例如:-
<form method="post" action="register">
<input type="text" name="name">
<input type="text" name="lastname">
</form>
如何在我的模型用户中请求姓名或姓氏
'use strict';
module.exports = function(User) {
// how i can receive requested of name or lastname for eg :-
console.log(req.name)
};
2.how 我可以将此数据插入我的 postgres table(table name :-register) 列 "name"
3.last 一个是如何区分 table name 和 model name using automigrate
module.exports = function(User) {
User.show = function(name,lastName) {
console.log(name);
console.log(lastName);
}
User.remoteMethod('show', {
accepts: [
{ arg: 'name', type: 'string' },
{ arg: 'lastName', type: 'string' }
],
returns: {arg: 'result', type: 'string'}
});
}
插入类似 :
var yourVar = name;
pg.connect(dbUrl, function(err, client, done) {
client.query(
'INSERT into yourTable(name) VALUES ()',
['title'],
function(err, result) {
if (err) {
console.log(err);
} else {
console.log('row inserted');
}
});
});
取决于您的 table 和日志凭据
编辑:
要使用自动迁移,您必须先:
在/server/datasources.json中创建数据源:
"mydb": {
"name": "mydb",
"connector": "postgresql"
}
连接到 /var/run/postgresql/.s.PGSQL.5432 中的 UNIX 域套接字:
{
"postgres": {
"host": "/var/run/postgresql/",
"port": "5432",
"database": "dbname",
"username": "dbuser",
"password": "dbpassword",
"name": "postgres",
"debug": true,
"connector": "postgresql"
}
}
在 /common/models/model.json 中定义您的模型,它应该如下所示:
var schema={
"name": "User",
"options": {
"idInjection": true,
"postgresql": {
"schema": "yourShema",
"table": "USER"
}
},
"properties": {
"id": {
"type": "Number",
"required": true,
"length": 64,
"precision": null,
"scale": null,
"postgresql": {
"columnName": "id",
"dataType": "integer",
"dataLength": 64,
"dataPrecision": null,
"dataScale": null,
"nullable": "NO"
}
},
"name": {
"type": "String",
"required": false,
"length": 40,
"precision": null,
"scale": null,
"postgresql": {
"columnName": "name",
"dataType": "character varying",
"dataLength": 40,
"dataPrecision": null,
"dataScale": null,
"nullable": "YES"
}
},
"lastName": {
"type": "String",
"required": false,
"length": 40,
"precision": null,
"scale": null,
"postgresql": {
"columnName": "lastName",
"dataType": "character varying",
"dataLength": 40,
"dataPrecision": null,
"dataScale": null,
"nullable": "YES"
}
}
}
那么你必须像这样在代码中调用 Model.automigrate()
或 Model.autoupdate()
:
ds.createModel(schema);
ds.autoupdate(schema) {
ds.discoverModelProperties('USER', function (err, props) {
console.log(props);
});
});
你肯定会改变一些东西,但我希望你能实现你想要的;)
大家好,我是 loopback 的新手,请问有人能帮我解决这个问题吗? 1. 我知道如何从环回服务器呈现 html 页面,例如:-
<form method="post" action="register">
<input type="text" name="name">
<input type="text" name="lastname">
</form>
如何在我的模型用户中请求姓名或姓氏
'use strict';
module.exports = function(User) {
// how i can receive requested of name or lastname for eg :-
console.log(req.name)
};
2.how 我可以将此数据插入我的 postgres table(table name :-register) 列 "name" 3.last 一个是如何区分 table name 和 model name using automigrate
module.exports = function(User) {
User.show = function(name,lastName) {
console.log(name);
console.log(lastName);
}
User.remoteMethod('show', {
accepts: [
{ arg: 'name', type: 'string' },
{ arg: 'lastName', type: 'string' }
],
returns: {arg: 'result', type: 'string'}
});
}
插入类似 :
var yourVar = name;
pg.connect(dbUrl, function(err, client, done) {
client.query(
'INSERT into yourTable(name) VALUES ()',
['title'],
function(err, result) {
if (err) {
console.log(err);
} else {
console.log('row inserted');
}
});
});
取决于您的 table 和日志凭据
编辑:
要使用自动迁移,您必须先:
在/server/datasources.json中创建数据源:
"mydb": {
"name": "mydb",
"connector": "postgresql"
}
连接到 /var/run/postgresql/.s.PGSQL.5432 中的 UNIX 域套接字:
{
"postgres": {
"host": "/var/run/postgresql/",
"port": "5432",
"database": "dbname",
"username": "dbuser",
"password": "dbpassword",
"name": "postgres",
"debug": true,
"connector": "postgresql"
}
}
在 /common/models/model.json 中定义您的模型,它应该如下所示:
var schema={
"name": "User",
"options": {
"idInjection": true,
"postgresql": {
"schema": "yourShema",
"table": "USER"
}
},
"properties": {
"id": {
"type": "Number",
"required": true,
"length": 64,
"precision": null,
"scale": null,
"postgresql": {
"columnName": "id",
"dataType": "integer",
"dataLength": 64,
"dataPrecision": null,
"dataScale": null,
"nullable": "NO"
}
},
"name": {
"type": "String",
"required": false,
"length": 40,
"precision": null,
"scale": null,
"postgresql": {
"columnName": "name",
"dataType": "character varying",
"dataLength": 40,
"dataPrecision": null,
"dataScale": null,
"nullable": "YES"
}
},
"lastName": {
"type": "String",
"required": false,
"length": 40,
"precision": null,
"scale": null,
"postgresql": {
"columnName": "lastName",
"dataType": "character varying",
"dataLength": 40,
"dataPrecision": null,
"dataScale": null,
"nullable": "YES"
}
}
}
那么你必须像这样在代码中调用 Model.automigrate()
或 Model.autoupdate()
:
ds.createModel(schema);
ds.autoupdate(schema) {
ds.discoverModelProperties('USER', function (err, props) {
console.log(props);
});
});
你肯定会改变一些东西,但我希望你能实现你想要的;)