是否可以使用[app-name]/api/models外部api文件夹中定义的模型?
我在应用程序根目录中创建了单独的文件夹,在那里我放置了应该每小时填充数据库的cron作业.我想重用我的api文件夹中定义的模型,但不知道如何做到这一点.
我看到我可以sails.lift在那个cron脚本里面做,但这对我来说似乎不太好.
有人经历过类似的事吗?我是node.js的新手,所以也许我错过了一些非常明显的东西.
有没有办法通过sails-mongo执行批量插入.因此,如果我将一个文档数组传递给模型的create方法,它会将该数组中的每个文档原子地插入到数据库中吗?
一个非常简单的问题,但我无法找到答案.所以在我的Sails应用程序中我有一个User模型,我试图创建一个默认值为False的布尔字段.有没有办法指定默认值,如某种默认属性等?
像这样的东西:
is_admin:{type:'boolean',默认值:'false'}
谢谢你的时间.
我在sails.js上使用Waterline ORM.limit并且sort在使用时不起作用groupby,但在不进行任何分组时工作正常.
例如
Model.find({
groupBy:['term'],
sum:['count'],
limit:20,
sort :'count DESC'}).exec(function(error,response){
if(error) res.json(error);
res.json(response);
});
Run Code Online (Sandbox Code Playgroud) 我遇到的问题与官方文档中的以下引用有关:
注意如果模型使用了与适配器的任何连接,则无论模型是否实际使用它们,都将在sails.lift上加载与该适配器的所有连接.在上面的示例中,如果模型配置为使用localMysql连接,则localMysql和remoteMysql都将尝试在运行时进行连接.因此,最好按环境拆分连接配置并将其保存到适当的特定于环境的配置文件中,否则请注释掉任何不希望处于活动状态的连接.
如何配置生产服务器的连接?
我的connections.js文件看起来像这样:
module.exports.connections = {
mongoDev: {
adapter: 'sails-mongo',
host: 'localhost',
port: 27017,
user: 'username',
password: 'password',
database: 'database'
},
mongoLive: {
adapter: 'sails-mongo',
host: 'host.mongolab.com',
port: 31681,
user: 'user',
password: 'password',
database: 'database'
}
};
Run Code Online (Sandbox Code Playgroud)
在我的环境配置文件我有:
development.js
module.exports = {
models: {
connection: 'mongoDev'
}
};
Run Code Online (Sandbox Code Playgroud)
production.js
module.exports = {
models: {
connection: 'mongoLive'
},
port: 3000,
};
Run Code Online (Sandbox Code Playgroud)
这适用于我的本地计算机,因为生产数据库服务器位于外部服务器上.在生产环境中我收到以下错误:
[Error: failed to connect to [localhost:27017]]
Run Code Online (Sandbox Code Playgroud)
如果我从连接对象中删除mongoDev对象,它可以工作.
我也尝试过使用adaptors.js,但这只会导致一些弃用错误.
$ sails -v
info: v0.9.9
Run Code Online (Sandbox Code Playgroud)
跑步时我会得到一些不同的东西 …
我是sails js的新手.我想在sails js中实现android和IOS的推送通知.
我正在使用带有mongodb的Sails js.
任何人都可以帮我解决配置或使用哪个插件在sails js中实现推送通知.
提前致谢.
我收到此错误:尝试使用mongoDB解除风帆应用程序时无法加载钩子('orm').这是我的connections.js文件:
module.exports.connections = {
mongodb: {
adapter : 'sails-mongo',
host : 'localhost',
port : 27017,
database : 'mydb1'
}
};
Run Code Online (Sandbox Code Playgroud)
这是我的models.js文件:
module.exports.models = {
connection: 'mongodb'
};
Run Code Online (Sandbox Code Playgroud)
这是我的local.js文件:
module.exports = {
connections: {
mongodb: {
host : 'localhost',
port : 27017,
database : 'mydb1'
}
}
}
Run Code Online (Sandbox Code Playgroud)
Sails v 0.10.1
知道为什么会发生这种情况吗?
谢谢
我在Nodejs和sails上都很新.我正在实现一个类似于Twitter的服务器.在用户模型中,应该有2个字段:跟随者和跟随者,并且2个字段是模型"用户"本身的关联.
我的问题是当模型只有1个关联时,无论是跟随者还是跟随者,它都有效.但是,如果包括跟随者和跟随者,则会出现错误.
代码是这样的:
module.exports = {
attributes: {
alias: {
type:'string',
required: true,
primaryKey: true
},
pwd: {
type: 'string',
required: true
},
follower: {
collection: 'user',
via: 'alias'
},
following:{
collection: 'user',
via: 'alias'
}
}
Run Code Online (Sandbox Code Playgroud)
代码会导致这样的错误:
usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/waterline-schema/lib/waterline-schema/references.js:115
throw new Error('Trying to associate a collection attribute to a model tha
^
Error: Trying to associate a collection attribute to a model that doesn't have a Foreign Key. user is trying to reference a foreign key in user
at References.findReference …Run Code Online (Sandbox Code Playgroud) 这是我的 Users.model:
module.exports = {
attributes: {
name: {
type: 'string',
required: true,
minLength: 3,
maxLength: 30
},
username: {
type: 'string',
required: true,
},
toJSON: function() {
var obj = this.toObject();
obj.link = sails.config.globals.baseUrl + sails.config.routes.user + obj.id;
return obj;
}
}
};
Run Code Online (Sandbox Code Playgroud)
我想要的是使用一些在模型中“预”计算的属性。我的解决方案是在 toJSON() 函数中注入 attr,但在视图中我必须使用:
<%= users.toJSON().link %>
Run Code Online (Sandbox Code Playgroud)
有没有办法为用户创建一个属性或一些方法?喜欢:
module.exports = {
attributes: {
name: {
type: 'string',
required: true,
minLength: 3,
maxLength: 30
},
myPersonalAttribute: function(){
return "Value"
}
}
Run Code Online (Sandbox Code Playgroud) 我在一个项目上使用SailsJS,我需要使用native()某些查询.我遇到的问题是我找不到从mongo集合find结果中实例化Waterline Model Object的正确方法.我一直在搜索有关此内容的信息,我发现的唯一内容如下:
var instance = new Model._model(mongo_result_item);
Run Code Online (Sandbox Code Playgroud)
这应该可以正常工作,但是当我这样做时instance.save(function(err, ins){});,由于"_id"字段,模型会抛出错误,应该是"id".
我已经看了sails-mongo代码,我发现对于"find"方法,他们这样做:
// Run Normal Query on collection
collection.find(where, query.select, queryOptions).toArray(function(err, docs) {
if(err) return cb(err);
cb(null, utils.normalizeResults(docs, self.schema));
});
Run Code Online (Sandbox Code Playgroud)
所以normalizeResults用"_id"属性和其他东西来做神奇的事.
我现在这样做的方式是要求sails-mongo utils.js文件可以访问此方法.
完整样本:
var mongoUtils = require('sails-mongo/lib/utils.js');
SampleModel.native(function(nativeErr, collection){
collection.find({ 'field' : value }).toArray(function(collectionErr, results){
if (!results || results.length == 0) return res.restfullInvalidFieldValue({ msg : 'INVALID_VALUE' });
var norm_results = mongoUtils.normalizeResults(results);
var instance = new SampleModel._model(norm_results[0]);
});
});
Run Code Online (Sandbox Code Playgroud)
有没有更好/正确的方法来实现这一目标?
我需要进行本机搜索,因为我发现使用字符串的Waterline find()方法存在问题,其中搜索应区分大小写.模型上的每个字符串字段都用作以下形式的正则表达式匹配:/ ^ {string} $/i …