Mongodb sails.js模型引用错误
我正在学习Sails.js,我有一个问题。 我使用“sails生成模型站点”命令创建了一个模型对象。 我在配置目录中为我的cronjob创建了init.js文件。 每次启动系统时,cronjob都从init.js开始 这是init.jsMongodb sails.js模型引用错误,mongodb,cron,sails.js,Mongodb,Cron,Sails.js,我正在学习Sails.js,我有一个问题。 我使用“sails生成模型站点”命令创建了一个模型对象。 我在配置目录中为我的cronjob创建了init.js文件。 每次启动系统时,cronjob都从init.js开始 这是init.js var fs = require('fs'), sails = require('sails'), async = require('async'); exports.initSite = function () { 'use stri
var fs = require('fs'),
sails = require('sails'),
async = require('async');
exports.initSite = function () {
'use strict';
sails.log.debug('init method start!');
async.waterfall([
function (callback) {
Site.find().exec(function (err, sites) {
if (err) {
callback(err);
}
if (sites) {
async.forEachSeries(sites,
function (siteData, callback2) {
siteData.remove(function (err) {
if (err) {
callback(err);
}
callback2();
});
}, function (err) {
if (err) {
callback(err);
}
callback();
});
} else {
callback();
}
});
},
function (callback) {
var jsonData = fs.readFile('./urls.json', 'utf8', function (err, datas) {
if (err) {
callback(err);
}
callback(null, datas);
});
},
function (datas, callback) {
var urls = JSON.parse(datas);
for (var key in urls) {
var value = urls[key];
var site = new Site({
siteId: value.id,
name: value.name,
url: value.url,
category: value.category
});
site.save(function(err){
if (err) {
callback(err);
}
});
}
callback();
}
],function(err){
if (err) {
sails.log.error('ERROR!');
}
sails.log.info('INIT OK');
});
};
我是这样从app.js中读到init.js的
在app.js中
require(__dirname + '/config/init').initSite();
但每次我启动应用程序时,控制台消息都会显示ReferenceError:Site未定义
我不知道为什么init.js不能读取“Site(model object)”
你的建议非常感谢我
(很抱歉我的英语不好……;)Sailsjs不会加载您的模型,直到它被“提升”。您需要首先从定义了app.js文件的目录中运行sails lift,然后才能运行此chron作业。帆装载后,您的所有模型都将显示为全局模型。应在sails提升后从app.js调用init.js中的代码。Sailsjs在“提升”之前不会加载您的模型。您需要首先从定义了app.js文件的目录中运行sails lift,然后才能运行此chron作业。帆装载后,您的所有模型都将显示为全局模型。应在sails提升后从app.js调用init.js中的代码。Sailsjs在“提升”之前不会加载您的模型。您需要首先从定义了app.js文件的目录中运行sails lift,然后才能运行此chron作业。帆装载后,您的所有模型都将显示为全局模型。应在sails提升后从app.js调用init.js中的代码。Sailsjs在“提升”之前不会加载您的模型。您需要首先从定义了app.js文件的目录中运行sails lift,然后才能运行此chron作业。帆装载后,您的所有模型都将显示为全局模型。init.js中的代码应该在sails提升后从app.js调用。这是基于对上述主题的讨论,因此它并不完全是一个“cron作业”,但它可以像一个作业一样执行,还可以访问sails.js提供的所有优秀功能,包括模型 您可以使用节点计划 这就是我所做的
module.exports.crontab = {
/*
* The asterisks in the key are equivalent to the
* schedule setting in crontab, i.e.
* minute hour day month day-of-week year
* so in the example below it will run every minute
*/
crons:function()
{
var jsonArray = [];
jsonArray.push({interval:’*/1 * * * * * ‘,method:’mytest’});
// add more cronjobs if you want like below
// but dont forget to add a new method…
//jsonArray.push({interval:’*/1 * * * * * ‘,method:’anothertest’});
return jsonArray;
},
// declare the method mytest
// and add it in the crons function
mytest: function(){
require(‘../crontab/mytest.js’).run();
}
/*
anothertest:function(){
require(‘../crontab/anothertest.js’).run();
}
*/
};
最后运行sails l,您应该会看到每分钟都有一条消息运行。这是基于对上述主题的讨论,因此它并不完全是一个“cron作业”,但它可以像一个作业一样执行,还可以访问sails.js提供的所有优秀功能,包括模型 您可以使用节点计划 这就是我所做的
module.exports.crontab = {
/*
* The asterisks in the key are equivalent to the
* schedule setting in crontab, i.e.
* minute hour day month day-of-week year
* so in the example below it will run every minute
*/
crons:function()
{
var jsonArray = [];
jsonArray.push({interval:’*/1 * * * * * ‘,method:’mytest’});
// add more cronjobs if you want like below
// but dont forget to add a new method…
//jsonArray.push({interval:’*/1 * * * * * ‘,method:’anothertest’});
return jsonArray;
},
// declare the method mytest
// and add it in the crons function
mytest: function(){
require(‘../crontab/mytest.js’).run();
}
/*
anothertest:function(){
require(‘../crontab/anothertest.js’).run();
}
*/
};
最后运行sails l,您应该会看到每分钟都有一条消息运行。这是基于对上述主题的讨论,因此它并不完全是一个“cron作业”,但它可以像一个作业一样执行,还可以访问sails.js提供的所有优秀功能,包括模型 您可以使用节点计划 这就是我所做的
module.exports.crontab = {
/*
* The asterisks in the key are equivalent to the
* schedule setting in crontab, i.e.
* minute hour day month day-of-week year
* so in the example below it will run every minute
*/
crons:function()
{
var jsonArray = [];
jsonArray.push({interval:’*/1 * * * * * ‘,method:’mytest’});
// add more cronjobs if you want like below
// but dont forget to add a new method…
//jsonArray.push({interval:’*/1 * * * * * ‘,method:’anothertest’});
return jsonArray;
},
// declare the method mytest
// and add it in the crons function
mytest: function(){
require(‘../crontab/mytest.js’).run();
}
/*
anothertest:function(){
require(‘../crontab/anothertest.js’).run();
}
*/
};
最后运行sails l,您应该会看到每分钟都有一条消息运行。这是基于对上述主题的讨论,因此它并不完全是一个“cron作业”,但它可以像一个作业一样执行,还可以访问sails.js提供的所有优秀功能,包括模型 您可以使用节点计划 这就是我所做的
module.exports.crontab = {
/*
* The asterisks in the key are equivalent to the
* schedule setting in crontab, i.e.
* minute hour day month day-of-week year
* so in the example below it will run every minute
*/
crons:function()
{
var jsonArray = [];
jsonArray.push({interval:’*/1 * * * * * ‘,method:’mytest’});
// add more cronjobs if you want like below
// but dont forget to add a new method…
//jsonArray.push({interval:’*/1 * * * * * ‘,method:’anothertest’});
return jsonArray;
},
// declare the method mytest
// and add it in the crons function
mytest: function(){
require(‘../crontab/mytest.js’).run();
}
/*
anothertest:function(){
require(‘../crontab/anothertest.js’).run();
}
*/
};
最后运行sails l,您应该会看到每分钟都有一条消息运行。感谢您的帮助。我解决了这个问题。我更改了bootstrap.js中initSite()的代码。cronjob也很管用。谢谢你的帮助。我解决了这个问题。我更改了bootstrap.js中initSite()的代码。cronjob也很管用。谢谢你的帮助。我解决了这个问题。我更改了bootstrap.js中initSite()的代码。cronjob也很管用。谢谢你的帮助。我解决了这个问题。我更改了bootstrap.js中initSite()的代码。克朗乔工作得很好。