Skip to content

依旧是multipart()用async出问题:Cannot read property 'length' of undefined #1500

Description

@newcomein

问题代码:

"use strict";
const fs = require('fs'),
    path = require('path'),
    crypto = require('crypto');
module.exports = app => {
    return class PostController extends app.Controller {
        async index() {
            const baseUrl = './app/storage/';
            const defaults = {
                flags: 'w',
                encoding: 'utf8',
                fd: null,
                mode: 0o666,
                autoClose: true
            };
            //头信息
            const headers = this.ctx.get('content-type');//application/x-www-form-urlencoded
            const files = this.ctx.multipart();//获取上传实例;
            let files2;
            while (files2 = await files) {
                if (files2._readableState.length) {
                    //秒传
                    const hash = crypto.createHash('md5');
                    files2.on('data', hash.update.bind(hash));
                    files2.on('end', () => {
                        //获取文件流对比数据库md5值
                        const fMd5 = hash.digest('hex');
                        //const a=this.ctx.service.sqlRead.get(1,2);
                        //console.log(a);
                    });
                    try {
                        const ws = fs.createWriteStream(baseUrl + new Date().toLocaleDateString() + '_' + Math.random() + files2.filename.slice(files2.filename.lastIndexOf('.')), defaults);
                        files2.pipe(ws);
                    }
                    catch (err) {
                        console.log(err)
                    }
                } else {
                    return
                }
            }
        }
    };
};

项目演示:https://github.com/newcomein/Wakeup-OS/tree/master/app/controller/post.js
直接打开https://github.com/newcomein/Wakeup-OS/tree/master/app/view/default/pc/index.html
点击上传文件,即可重现这个问题

报错信息:

2017-10-10 00:55:51,365 ERROR 10740 [-/127.0.0.1/-/7ms POST /post] nodejs.TypeError: Cannot read property 'length' of undefined
    at PostController.index (E:\WebstormProjects\Wakeup-OS\app\controller\post.js:21:42)
    at <anonymous>
headers: { 'Access-Control-Allow-Origin': '*' }
pid: 10740
hostname: rockeyµÄ±Ê¼Ç±¾


Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions