簡體   English   中英

沒有方法錯誤(在異步函數內部訪問方法)

[英]No Method Error ( accessing method inside async function )

我正在開發Zappa應用程序,並且遇到了我認為JavaScript知識的瓶頸。 問題是我找不到在異步函數內部訪問函數的方法。

當我使用CoffeeScript時,我記得使用that = @( that = this )類型的作品。 我也嘗試了=>而不是->但是沒有成功。

這是錯誤消息: TypeError: Object #<Object> has no method 'convertUploadedImage'

我該如何克服?

問題在於:

        .on "end", () ->
          file.end()
          console.log "log inside"
          console.log that.constructor.name
          that.convertUploadedImage file

預先感謝。

整個代碼(或至少我認為重要的內容):

{@app} = require('./node_modules/zappajs') ->
  @post "/collage/upload", (req, res) ->
    # We might be uploading two types of files
    #   1. web form 
    #   2. as a link
    #

    # Check for the upload URL to exist
    url = req.body.upload_url # if req.body.upload_url
    console.log "URL is - #{url}"
    # URL is present - Retrieve the proper file
    if url 
      # setting up for download
      http     = require 'http'
      fileName = url.split('/').pop()
      file     = fs.createWriteStream __dirname + "/public/images/uploads/temp/#{fileName}"

      that = @
      console.log that.constructor.name
      # download the file
      request = http.get "#{url}", ( res ) ->
        res.on "data", (data) ->
          file.write data
        .on "end", () ->
          file.end()
          console.log "log inside"
          console.log that.constructor.name
          that.convertUploadedImage file

    # URL is not present
    else
      file = req.files.files[0]
      @convertUploadedImage file

  convertUploadedImage: ( file, deleteOriginal = false ) ->    
    # Checking file type
    console.log file.type

查看文檔的這一部分( http://coffeescript.org/#fat-arrow ),粗箭頭應該起作用:

  request = http.get "#{url}", ( res ) =>
    res.on "data", (data) ->
      file.write data
    .on "end", () =>
      file.end()
      console.log "log inside"
      console.log that.constructor.name
      @convertUploadedImage file

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM