【问题标题】:Expressjs - Cannot set headers after they are sent to the clientExpressjs - 将标头发送到客户端后无法设置标头
【发布时间】:2019-03-01 05:37:36
【问题描述】:

虽然尝试其他人的解决方案似乎并不能为我解决此错误。我想做的是for each I in the array 我的数组来自下面

novel.ts

export let indexList = (req: Request, res: Response) => {
    novel.getAllDocuments((data) => {
        const novelObj = Convert.toNovelObj(data);

        res.render(`novels/all`, {
            title: `List of novels`,
            array: {
                name: novelObj.novelName,
                author: novelObj.novelAuthor,
                id: novelObj._id,
                img: novelObj.novelCoverArt,
                tags: novelObj.novelTags
            }
        });
    });
};

novelObj 将我的 MongoDB 从 json 转换为可以在下面看到的对象

RiNovel.ts

public getAllDocuments(callback: (data) => void) {
        var chapterInfoModel = mongoose.model('Novels', RiNovelcheme);
        chapterInfoModel.collection
            .find()
            .stream()
            .on('data', function(doc) {
                const novelObj = Convert.novelObjToJson(doc);
                return callback(novelObj);
            })
            .on('error', function(err) {
            })
            .on('end', function() {
            });
    } 

all.pug

each i in array
    p= i.name

错误是当我的集合中有多个文档时,我将如何解决这个问题

【问题讨论】:

标签: node.js mongodb typescript express pug


【解决方案1】:

我发现这是我的解决方案

export let indexList = (req: Request, res: Response) => {
    var chapterInfoModel = mongoose.model('Novels', RiNovelcheme);
    chapterInfoModel.find().exec((err, novel) => {
        if (err) {
            res.send(err);
        }
        res.render('novels/all', {
            title: 'All Novels',
            name: novel
        });
    });
};

【讨论】:

    猜你喜欢
    • 2019-08-10
    • 1970-01-01
    • 1970-01-01
    • 2019-01-23
    • 2021-11-30
    • 2021-06-18
    • 2022-01-14
    • 2021-04-07
    • 1970-01-01
    相关资源
    最近更新 更多