种子nodejs表达heroku应用程序

时间:2017-11-17 12:52:02

标签: node.js express heroku seed



mongoose.connect('mongodb://localhost:27017/smartphones');
mongoose.connection.on('error', console.error.bind(console, 'MongoDB connection error:'));

var smartphones = [
    new Smartphone({
        title: "V3",
        usp: "20MP Softlight Camera",
        image_path: "/image/phone_v3max.png",
        qty: 1,
        price: 200
    }),
    new Smartphone({
        title: "V5",
        usp: "Feel the Real Speed",
        image_path: "/image/phone_v5.png",
        qty: 1,
        price: 450
    })
];

var done = 0;
for(var i = 0; i < smartphones.length; i++) {
    smartphones[i].save(function(err, result) {
        done++;
        if(done === smartphones.length) {
            exit();
        }
    });
}

function exit() {
    mongoose.disconnect();
}
&#13;
&#13;
&#13;

当我在Nodejs App上本地工作时,我可以通过运行命令行运行并播种包含在项目中的数据文件(smartphoneIndex-seeder.js)。但是当应用程序被推送到Heroku时应该怎么做呢,我怎样才能从那里播种数据呢?该应用程序正在运行,但不幸的是,由种子数据运行的页面不会显示在我的页面上。任何人都知道如何连接种子日期,以便我的整个应用程序将正常运行。

1 个答案:

答案 0 :(得分:0)

听起来我想要某种预启动过程。您可能希望在告诉应用程序开始侦听端口之前编写一些执行的代码。此代码将查看数据库,查看是否存在任何数据,如果不存在,则会调用某些代码(例如将smartphoneIndex-seeder函数推送到应用程序中。

一旦做出决定,您就可以调用正常的app.listen()快递代码。