-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathloopback-override.js
More file actions
80 lines (77 loc) · 1.87 KB
/
loopback-override.js
File metadata and controls
80 lines (77 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
var findProductInSolrDB = function(id, cb) {
Product.app.models.image.getDataSource().connector.connect(function(err, db) {
var product = db.collection('product');
console.log(typeof id);
var productId = Product.app.models.image.getDataSource().ObjectID(id);
console.log(typeof productId);
product.findOne({
_id: productId
}, function(err, data) {
if (data) {
data.id = data._id;
}
// console.log(data);
cb(err, data)
})
});
}
var formatData = function(data, cb) {
var interaldata = data;
cb(null, interaldata)
}
Product.findByIdCustom = function(id, filter, cb) {
//https://stackoverflow.com/questions/30255524/loopback-rest-findbyid-doesnt-work-well
console.log("id ", " id", id);
Product.findById(id, filter, function(err, data) {
console.log(data);
if (err) {
return cb(err)
}
if (data) {
return formatData(data, cb)
// return cb(null, data)
}
if (!data) {
// check into solr database and return data
console.log("check into solr database and return data");
// cb(err, data)
findProductInSolrDB(id, cb)
}
});
}
//Define remote method
Product.remoteMethod(
'findByIdCustom', {
description: 'Find a model instance by id from the data source.',
accessType: 'READ',
accepts: [{
arg: 'id',
type: 'string',
description: 'Model id',
required: true,
http: {
source: 'path'
}
},
{
arg: 'filter',
type: 'object',
description: 'Filter defining fields and include'
}
],
returns: {
arg: 'data',
type: 'user',
root: true
},
http: {
verb: 'get',
path: '/:id'
},
rest: {
after: Product.convertNullToNotFoundError
},
isStatic: true
}
);
Product.disableRemoteMethod('findById', true);