Express.js-将标头发送到客户端后无法设置标头

时间:2019-03-01 05:37:37

标签: node.js mongodb typescript express pug

尽管尝试其他人的解决方案似乎并不能解决我的问题。我想做的是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

错误是当我的收藏夹中有多个文档时,我该如何解决

1 个答案:

答案 0 :(得分:0)

我发现这是我的解决方案

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
        });
    });
};