在环回中感到困惑

时间:2018-02-23 10:17:03

标签: node.js loopbackjs strongloop

你好,我是新手环回所以请,任何人都可以帮助我这一点 1.我知道如何从loopback服务器渲染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.我可以在“name”列中将此数据插入我的postgres表(表名:--register) 3.一个是如何使用automigrate来区分表名和模型名称

1 个答案:

答案 0 :(得分:1)

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 ($1)', 
        ['title'], 
        function(err, result) {
            if (err) {
                console.log(err);
            } else {
                console.log('row inserted');
            }
        });        
});

取决于您的表和日志凭据

修改:

要使用自动迁移,您必须先:

在/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);
    });
});

你肯定会改变一些事情,但我希望你能实现你想要的东西;)