mongodb - express中插入数据库后不能跳转

查看:141
本文介绍了mongodb - express中插入数据库后不能跳转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

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

这是什么原因?

解决方案

刚刚看了下,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');

这篇关于mongodb - express中插入数据库后不能跳转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆