Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
newcomein opened this issue Oct 9, 2017 · 19 comments · Fixed by #1518
Closed

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

newcomein opened this issue Oct 9, 2017 · 19 comments · Fixed by #1518

Comments

@newcomein
Copy link

问题代码:

"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µÄ±Ê¼Ç±¾


@newcomein
Copy link
Author

newcomein commented Oct 9, 2017

>△< 实在是没办法了惹 求官方搭救

@popomore
Copy link
Member

popomore commented Oct 9, 2017

Stream 怎么取 length

@popomore
Copy link
Member

popomore commented Oct 9, 2017

你可以先上传再改名,或用 uuid

@newcomein
Copy link
Author

在files2 = yeild files情况下 同样为steam却可以获取length,很懵逼( p′︵‵。)

我没辙了,现在连数据都获取不了,所以没法报错流
可否提供一个实例修改代码?

@dead-horse
Copy link
Member

ctx.multipart 返回的好像不是一个 promise 对象, @newcomein 你是 yield 正常,await 不行对吧?

@newcomein
Copy link
Author

@dead-horse 是的 就是这种情况 ・(PД`q。)・゜・ 很奔溃 感觉整个人的灵魂都快丢了 (/TДT)/

@atian25
Copy link
Member

atian25 commented Oct 10, 2017

可以先套个 co 吧,回头 egg-multipart 可以支持下 Promise

@newcomein
Copy link
Author

@atian25 thanks ♪(^∀^●)ノ 十分感谢 我尝试一下
@popomore @dead-horse 感谢各位女装大佬~~~阿里嘎多ฅ’ω’ฅ

@popomore
Copy link
Member

chan 返回的是一个 thunk,能返回 promise?

https://github.com/brentburg/chan/blob/master/lib/channel.js#L99

@dead-horse
Copy link
Member

去提个 PR 看看

@popomore
Copy link
Member

brentropy/chan#18

@popomore
Copy link
Member

要不要先 wrap 下

@dead-horse
Copy link
Member

我看了一下,有点麻烦,它用到了 thunk 可重复调用的特性,改成 promise 写法肯定要变。我先 wrap 一个用另外的形式支持 promise 吧

@dead-horse dead-horse self-assigned this Oct 12, 2017
@atian25
Copy link
Member

atian25 commented Oct 12, 2017

一年前的。。。估计不改了吧

@dead-horse
Copy link
Member

cojs/busboy#37

@hello2dj
Copy link

很无奈是么?我也遇到了,于是就换成yield来获取。。。

@popomore
Copy link
Member

这个已经改了

@atian25
Copy link
Member

atian25 commented Oct 17, 2017 via email

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

Successfully merging a pull request may close this issue.

5 participants