mongodb - express插入資料庫後不能跳轉
某草草
某草草 2017-05-02 09:22:26
0
2
550
exports.adduser = function(db) {
    return function(req, res) {
        // Get our form values. These rely on the "name" attributes
        var userName = req.body.username;
        var userEmail = req.body.useremail;
        var collection = db.get('users');
        var llll = collection.insert({
            "userid": userName,
            "password": userEmail,
        })
        if (llll) {
            res.location('http://baidu.com');
        } else {
            res.send("error")
        }
    }
};

資料庫是插入成功的,但是不會正確跳轉至到百度
報錯是 500 TypeError: res.location is not a function

這是什麼原因?

某草草
某草草

全部回覆(2)
Ty80

剛剛看了下,res.location是设置location請求頭的

res.location('/foo/bar');
res.location('foo/bar');
res.location('http://example.com');
res.location('../login');
res.location('back');

可以使用與 res.redirect()里相同的urls

舉個例子,如果你的程式根位址是/blog, 下面的代码会把 location 请求头设置为/blog/admin:

res.location('admin');

下面是重點

一般後端跳轉都是用res.redirect([status], url),(狀態可選,預設為302):

res.redirect('/foo/bar');
res.redirect('http://example.com');
res.redirect(301, 'http://example.com');
伊谢尔伦

看下你是怎麼呼叫這個方法的,可能是res沒有正確傳入使用

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!