2017-08-27 76 views
0

我想知道這個錯誤的性質?任何想法調試將非常感激!續集基本錯誤:null

Unhandled rejection SequelizeBaseError: null 
    at /Users/master/node_modules/sequelize/lib/instance-validator.js:74:14 
    at tryCatcher (/Users/master/node_modules/bluebird/js/release/util.js:16:23) 
    at Promise._settlePromiseFromHandler (. 

我想我通過在所有需要的字段類似如下:

app.post("/api/posts", function(req, res) { 
    console.log(req.body); 
    db.Post.create({ 
    image: req.body.image, 
    caption: req.body.caption, 
    tags: req.body.tags 
    }).then(function(dbPost) { 
    res.redirect('/'); 
    console.log("posted!") 
    }); 
}); 

檢查allowNotNull,數據類型確定,等等

這裏是我的模型:

var Post = sequelize.define("Post", { 
    caption: { 
     type: DataTypes.TEXT, 
     allowNull: false, 
     validate: { 
     len: [1] 
     } 
    }, 
    likes: { 
    type: DataTypes.INTEGER, 
    defaultValue: 0 
    }, 
    image: { 
    type: DataTypes.STRING, 
    allowNull: false 
    }, 
    tags: { 
    type: DataTypes.STRING 
    } 
} 

回答

1

這是一個典型的Unhandled Promise Rejection Error,是由於你沒有一個.catch.then後:

app.post("/api/posts", function(req, res) { 
    console.log(req.body); 
    db.Post.create({ 
    image: req.body.image, 
    caption: req.body.caption, 
    tags: req.body.tags 
    }).then(function(dbPost) { 
    res.redirect('/'); 
    console.log("posted!") 
    }).catch(function(error) { 
    // Handle the error here 
    console.error(error); 
    }); 

。在你的代碼中的錯誤,但由於承諾的異步性,並且你不要再追你實際上並不瞭解它是什麼錯誤。

+0

你爲我釘了!高度讚賞你的時間! –

+0

在catch方塊中添加調用'next'函數:'catch(err){... next(err)}',因爲在你的例子中,服務器在錯誤的情況下不會發送響應。 – alexmac

+0

@alexmac因此爲什麼我添加了評論'//在這裏處理錯誤。由於我不知道莫哈克想要如何處理他的錯誤,所以我把這留給他決定。 – Svenskunganka