res.sendFile-强制转换为对象的值

时间:2019-08-24 16:42:55

标签: express mongoose

我正在使用ejs模板,但是我希望用户能够上载html文本。

我尝试使用ejs res.sendFile;进行此操作,ejs返回表单代码本身而不是命名的html文件。

html浏览器在浏览器中独立启动。

所以现在我正在尝试 'Cast to ObjectId failed for value "uploadBlog" at path "_id" for model "Blog"` ,但是即使使用简单的html,我也会收到错误消息:

blogUpload(req, res, next) {
    res.sendFile(path.resolve(__dirname, '../public/lorem.html', ));
},

尽管我使用的是mongo,但未在此路由,此控制器或html文件中调用它。我应该在哪里寻找问题?

控制器

    <!DOCTYPE html>

<head>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css">
    <script src="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.js"></script>
</head>
<h1>New Blog!</h1>
<div class="card">
    <div id="description2">
        <button id="upload">Upload New Blog</button>
    </div>
    <div id="static"></div>
    <script>
    document.getElementById('upload').addEventListener('click', uploadBlog);
    let description = document.getElementById('description');

    function uploadBlog(event) {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', 'lorem.html', true);
        xhr.responseType = "text";
        xhr.onload = function() {
            console.log('READYSTATE: ', xhr.readyState);
            console.log('readyState', xhr.status);
            if (this.status == 200) {
                let res = xhr.responseText;
                document.getElementById('static').innerHTML = this.responseText;

            } else if (this.status = 404) {
                document.getElementById('description').innerHTML = 'Not    Found';
                }
             }
            xhr.onerror = function() {
                console.log('Request Error...' + error);
            }
            xhr.send();
        }
    </script>
</div>

HTML

len(map_list)=9

............................................... ..

0 个答案:

没有答案