我正在使用LoopBack ver.1.6并使用以下数据源配置运行开发的本地mongoDB服务器:
"mongodb": {
"defaultForType": "mongodb",
"connector": "loopback-connector-mongodb",
"database": "xxxdbname",
"host": "localhost",
"port": "27017"
},
Run Code Online (Sandbox Code Playgroud)
现在我想部署到Heroku但我不知道如何配置数据源指向MongoLab数据库,因为它有一个动态生成的连接字符串:
来自Heroku dox:
var mongo = require('mongodb');
var mongoUri = process.env.MONGOLAB_URI ||
process.env.MONGOHQ_URL ||
'mongodb://localhost/mydb';
mongo.Db.connect(mongoUri, function (err, db) {
db.collection('mydocs', function(er, collection) {
collection.insert({'mykey': 'myvalue'}, {safe: true}, function(er,rs) {
});
});
});
Run Code Online (Sandbox Code Playgroud)
那么我需要对我的数据源JSON进行哪些更改来映射Heroku连接字符串?
如何从模型挂钩中访问提出请求的用户的详细信息
Comment.beforeSave = function(next,com) {
//Want to add 2 more properties before saving
com.added_at = new Date();
com.added_by = //How can i set the user id here ??
//In case of a Remote hook i have ctx in param and i can get user id like this ctx.req.accessToken.userId; But in Model Hook how can i do the same?
next();
};
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?我尝试使用远程钩子作为主要项目
MainItem.beforeRemote('**', function(ctx, user, next) {
if(ctx.methodString == 'leave_request.prototype.__create__comments'){
ctx.req.body.added_by = ctx.req.accessToken.userId;
ctx.req.body.added_at = new Date();
console.log("Added headers as .."+ctx.req.body.added_by); …Run Code Online (Sandbox Code Playgroud) 我的问题是我无法弄清楚如何使用LoopBack后端在一个请求中获得多级关系结构.我有3种型号:Continent,Country,County.我想做的是获得一个大陆,并接收所有国家和所有国家.
他们之间的关系:
ContinenthasMany Country,并且 Country属于ContinentCountryhasMany County,并且 County属于Country所以REST api调用/api/Continent/1返回
{
"id": 1
"name":"Europe"
}
Run Code Online (Sandbox Code Playgroud)
现在,我希望得到所有的国家和县Continent,所以我做了一个查询/api/Continent/1?filters[include]=country
不过,我还没有得到这些.
我应该进行什么样的查询才能获得包含关系级别的列表?像这样:
{
"id": 1,
"name": "Europe",
"country": [
id: 1,
name:"United Kingdom",
county:[
{id:1,name:"Avon"},
{id:2,name:"Bedfordshire"},
...
],
...
]
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!
我正在寻找一种方法来自定义StrongLoop LoopBack HTTP响应代码和标头.
我想遵守一些关于REST API的公司业务规则.
对于JSON中描述的模型,典型情况是让HTTP响应POST请求,使用代码201 +标头Content-Location(而不是没有Content-Location标头的loopback的默认响应代码200).
是否可以使用LoopBack来做到这一点?
我有一个问题我在Loopback的文档中找不到答案.
说我有模特Company和模特Employee.它Company和它之间有1Xn的关系Employees.当/api/Employees被调用时,服务器将返回所有员工.
我只想返回与请求列表的用户在同一公司的员工列表.
为此,我创建了一个远程钩子
Employee.beforeRemote('find', function(context, modelInstance, next) {
var reject = function() {
process.nextTick(function() {
next(null, false);
});
};
// do not allow anonymous users
var userId = context.req.accessToken.userId;
if (!userId) {
return reject();
}
//I get the details of the user who sent the request
//to learn which company does he belong to
Employee.findById(userId, function(err, user) {
if(!context.req.query.filter) context.req.query.filter={};
context.req.query.filter.where = {brandId:user.companyId};
console.log(context.req.query);
next();
});
});
Run Code Online (Sandbox Code Playgroud)
我认为这应该每次都有用,但是看起来它只有当find已经有一些像include这样的查询过滤器时才会起作用 - …
我的项目包括web和android客户端.我想结合Google跨平台登录和环回第三方登录.
在我的场景中,我永远不会要求用户名和密码.用户仅在网络和Android应用上使用Google登录按钮进行身份验证和授权.
让我们假设,这是您第一次通过我的网站登录Google登录.在环回第三方方案中,如果您不在db上,则会创建一个帐户对应的提供者和外部ID.(在这种情况下,提供商是谷歌和外部ID是您唯一的谷歌ID).这是针对web,loopback-example-passport
所以,假设上面的android场景.您只需点击Google登录按钮,然后环回服务器就可以执行以上操作(如网络)
这是否有内置的环回方式?如果没有,我如何正确地将此场景集成到环回中?
我正在使用nodejs的loopback框架.
是否可以一次连接多个数据库.
例如,我有两个不同的数据库.
1. Mysql Database - A
2. Postgresql - B
Run Code Online (Sandbox Code Playgroud)
有些页面从A数据库获取数据,有些页面需要从B数据库获取数据.有可能这样做吗?
更多细节:
假设我们有两个模块.一个模块与MySQL交互,另一个模块与postgreSQL交互.
我有一个问题,当我将两个对象类型作为远程方法参数传递时,第一个参数被第二个参数覆盖.以下是代码和结果.我怎么能不让第二个参数不覆盖第一个参数呢?
module.exports = (Model) => {
Model.calculate = (primary, secondary) => {
console.log(JSON.stringify(primary, null, 2));
console.log(JSON.stringify(secondary, null, 2));
return new Promise((resolve, reject) => {
resolve({ Model: calculator.calculate() });
});
};
Model.remoteMethod('calculate', {
accepts: [
{ arg: 'primary', type: 'object', http: { source: 'body' } },
{ arg: 'secondary', type: 'object', http: { source: 'body' } }
],
returns: {arg: 'Result', type: 'string'}
});
};
Run Code Online (Sandbox Code Playgroud)
当我在控制台记录JSON对象primary和secondary后传入主参数{"name":"Tom"}和辅助参数{"name:"Joe"}时,我得到了结果.
primary
{
"name": "Joe" <--- WHY?!
}
secondary
{
"name: "Joe"
}
Run Code Online (Sandbox Code Playgroud)
如你所见,汤姆被乔覆盖了.
我读的是embedsMany(在非关系数据库的情况下),把嵌入式型号父模型文件内.在hasMany创建子模型的新集合时,在父集合和子集合之间创建关系.怎么样referencesMany?
我们使用strongloop流程管理器在生产盒上运行我的环回应用程序,并使用它sl-deploy来部署代码.
我们正面临以下问题: -
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory
Run Code Online (Sandbox Code Playgroud)
在谷歌搜索我发现上述问题可以通过增加max_old_space_size节点选项值以某种方式解决.
我做了以下更改来运行应用程序,并在sl-pm进程之前传递了此变量,但仍面临同样的问题,并发现我的slc进程仍然使用默认值max_old_space_size
/opt/node/bin/node **--max_old_space_size=3072**
/opt/node/lib/node_modules/strong-pm/bin/sl-pm.js --listen 8701 --base
/var/lib/strong-pm --base-port 3000 --driver direct
Run Code Online (Sandbox Code Playgroud) loopbackjs ×10
strongloop ×10
node.js ×3
javascript ×2
android ×1
arguments ×1
heroku ×1
http-headers ×1
loopback ×1
mongodb ×1
mysql ×1
object ×1
overwrite ×1
postgresql ×1