Discuss / JavaScript / 终于好了

终于好了

Topic source

终于弄好了,几个修改的地方记录一下: 一是要使用作者在github中的代码,文中的代码片段不是最新的会有问题。 二是要在运行node init-db.js之前设置NODE_ENV,win下是set NODE_ENV=test。linux和mac是export命令。

代码修改包括: process.exit(0)确实要在Promise的then或catch中执行

model.sync().then(()=>{
    console.log('sync done');
    process.exit(0);
}).catch((e)=>{
    console.log('failed with: '+e);
    process.exit(0);});

然而,如果你的mode.sync()以及db.sync()没有返回Promise的话,这段代码会报错的,所以

module.exports.sync = () => {
    return db.sync();
};
var exp = {
    defineModel: defineModel,
    sync: () => {
        // only allow create ddl in non-production environment:
        if (process.env.NODE_ENV !== 'production') {
            console.log('db.js - sync() called');
            return sequelize.sync({ force: true });
        } 
        ... ...
    }
};

这两个地方修改得比较少,就是加了return而已。


  • 1

Reply