环回中的JSON模型

时间:2018-04-18 10:39:00

标签: loopbackjs loopback loopback-address

我想从我的customer表中返回这样的模型。

{
  "1521544526409": {
    "address": "",
    "city": "",
    "company": "Atoy oy",
    "email": "henry.tuohimaa@atoy.fi",
    "forename": "Henry",
    "moreinfo": "",
    "note": "Customer related notes",
    "postalcode": "",
    "surname": "Tuohimaa",
    "tel": "0503383882"
  },
  "1521544680834": {
    "address": "",
    "city": "",
    "company": "",
    "email": "",
    "forename": "dsfdfs",
    "note": "",
    "postalcode": "",
    "surname": "sdffd",
    "tel": "fdsdsf"
  },
} 

有人能告诉我的模特应该是什么样的吗?

目前的型号如下。

{
  "name": "customer",
  "plural": "customer",
  "base": "Model",
  "idInjection": false,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "Id": {
      "type": "number",
      "id": true
    },
    "FirstName": {
      "type": "string"
    },
    "LastName": {
      "type": "string"
    },
    "Company": {
      "type": "string"
    },    
  },
  "validations": [],
  "acls": [],
  "methods": {}
}

我的 customer.js 文件。

const connector = app.dataSources.tovio.connector;

const sql = 'select * from customer INNER JOIN \
            address ON address.Id = customer.AddressId';
module.exports = function (Customer) {
    Customer.getDetails = function (data, cb) {
        connector.execute(sql, null, (err, instance) => {            
            cb(null, instance);
        });
    };
Customer.remoteMethod(
        'getDetails', {
            http: { path: '/getdetails', verb: 'get' },
            accepts: { arg: 'Id', type: 'number', http: { source: 'query' } },
            returns: { type: 'array', root: true }
        }
    );
}

我的网址如下。 - http://localhost:3000/api/customer/getdetails

并且回复是

    [
  {
    "Id": 1,
    "FirstName": "Lindsay",
    "LastName": "Baxter",
    "Company": "Pearl Architectural Design",
    "Email": "laron_jones",
    "Telephone": "773-617-5179",
    "Note": "Note1",
    "MoreDetails": "More1kjk",
    "AddressId": 1,
    "IsActive": 0,
    "Created": "2018-04-18T04:38:55.000Z",
    "Creator": null,
    "Modified": "2018-04-18T04:38:55.000Z",
    "Modifier": null,
    "Address": "4267 Cherry Ridge Drive",
    "City": "Webster",
    "PostalCode": "14580"
  },
  {
    "Id": 1,
    "FirstName": "James",
    "LastName": "Baxter",
    "Company": "Hudson",
    "Email": "evert.ksd",
    "Telephone": "484-319-7836",
    "Note": "Note3",
    "MoreDetails": "More3",
    "AddressId": 1,
    "IsActive": 0,
    "Created": "2018-04-18T04:38:55.000Z",
    "Creator": null,
    "Modified": "2018-04-18T04:38:55.000Z",
    "Modifier": null,
    "Address": "4267 Cherry Ridge Drive",
    "City": "Webster",
    "PostalCode": "14580"
  },
]

1 个答案:

答案 0 :(得分:0)



// customer.js
module.exports = function (Customer) {
  Customer.getDetails = function (id, done) {
    Customer.findById(id, function (err, customer) {
      if (err || !customer) return done(err, customer)

      let res = {
        // now you have the customer object, build your response
      }

      done(null, res)
    })
  }
  
  Customer.remoteMethod(
    'getDetails',
    {
      path: '/getdetails/:id', verb: 'get' },
      accepts: [
        {arg: 'id', type: 'number', required: true},
      ],
      returns: { type: 'array', root: true }
    } 
  )
}