Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 错误:enoint:即使Firebase云函数中存在文件,也没有这样的文件或目录_Javascript_Node.js_Firebase_Google Cloud Functions_Fs - Fatal编程技术网

Javascript 错误:enoint:即使Firebase云函数中存在文件,也没有这样的文件或目录

Javascript 错误:enoint:即使Firebase云函数中存在文件,也没有这样的文件或目录,javascript,node.js,firebase,google-cloud-functions,fs,Javascript,Node.js,Firebase,Google Cloud Functions,Fs,我对云功能比较陌生,已经尝试解决这个问题有一段时间了。本质上,只要有一个完整的上传到Firebase云存储,就会调用我试图编写的函数。但是,当函数运行时,有一半时间会出现以下错误: The following error occured: { Error: ENOENT: no such file or directory, open '/tmp/dataprocessing/thisisthefilethatiswritten.zip' errno: -2, code: 'ENOEN

我对云功能比较陌生,已经尝试解决这个问题有一段时间了。本质上,只要有一个完整的上传到Firebase云存储,就会调用我试图编写的函数。但是,当函数运行时,有一半时间会出现以下错误:

The following error occured:  { Error: ENOENT: no such file or directory, open '/tmp/dataprocessing/thisisthefilethatiswritten.zip'
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/tmp/dataprocessing/thisisthefilethatiswritten.zip' }
代码如下:

const functions = require('firebase-functions');
const admin = require('firebase-admin')
const inspect = require('util').inspect
const path = require('path');
const os = require('os');
const fs = require('fs-extra');

const firestore = admin.firestore()
const storage = admin.storage()

const runtimeOpts = {
    timeoutSeconds: 540,
    memory: '2GB'
  }

const uploadprocessing = functions.runWith(runtimeOpts).storage.object().onFinalize(async (object) => {
    const filePath = object.name
    const fileBucket = object.bucket
    const bucket_fileName = path.basename(filePath);
    const uid = bucket_fileName.match('.+?(?=_)')
    const original_filename = bucket_fileName.split('_').pop()

    const bucket = storage.bucket(fileBucket);
    const workingDir = path.join(os.tmpdir(), 'dataprocessing/');
    const tempFilePath = path.join(workingDir, original_filename);

    await fs.ensureDir(workingDir)

    await bucket.file(filePath).download({destination: tempFilePath})

    //this particular code block I included because I was worried that the file wasn't
    //being uploaded to the tmp directly, but the results of the function 
    //seems to confirm to me that the file does exist.

    await fs.ensureFile(tempFilePath)
        console.log('success!')
        fs.readdirSync(workingDir).forEach(file => {
            console.log('file found: ', file);
            });
        console.log('post success')
        fs.readdirSync('/tmp/dataprocessing').forEach(file => {
            console.log('tmp file found: ', file);
    })


    fs.readFile(tempFilePath, function (err, buffer) {
        if (!err) {
            //data processing comes here. Please note that half the time it never gets into this
            //loop as instead it goes into the else function below and outputs that error.
        }
        else {
            console.log("The following error occured: ", err);
        }
    })
    fs.unlinkSync(tempFilePath);
    return
})
module.exports = uploadprocessing;
我尝试了很多不同的方法,奇怪的是,当我将代码添加到“if(!err)”(由于错误,它实际上没有运行)中时,它只是随意地开始工作,有时非常一致,但当我添加不同的代码时,它停止工作。我本以为问题是由我添加的代码引起的,但当我只是更改/添加/删除注释时,错误就出现了。。。这在技术上应该不会对功能运行产生影响


有什么想法吗?提前谢谢!!!:)

fs.readFile
是异步的,并立即返回。一段时间后,您的回调函数将与缓冲区的内容一起调用。这意味着
fs.unlinkSync
将在读取文件的同时删除该文件。这意味着您实际上有一个竞争条件,并且可能在读取该文件之前将其删除


代码应该等到读取完成后再继续删除。也许您想改为使用。

fs.readFile
是异步的,会立即返回。一段时间后,您的回调函数将与缓冲区的内容一起调用。这意味着
fs.unlinkSync
将在读取文件的同时删除该文件。这意味着您实际上有一个竞争条件,并且可能在读取该文件之前将其删除


代码应该等到读取完成后再继续删除。也许您想改用。

只是为了澄清一下,当我运行此代码时,出现了特定错误,请确保refile和两个readdirSync函数都输出了正确的文件,以证明它确实存在……仅供参考:您不必对您的问题发表评论即可添加信息。请直接编辑问题。请澄清,当我运行此代码并出现特定错误时,请确保refile和两个readdirSync函数都输出正确的文件,以证明它确实存在……仅供参考:您不必对问题发表评论即可添加信息。直接编辑这个问题。非常感谢道格!先生,你是英雄中的英雄!我不敢相信我错过了,但将其更改为readFileSync效果很好:)谢谢!:)多谢你,道格!先生,你是英雄中的英雄!我不敢相信我错过了,但将其更改为readFileSync效果很好:)谢谢!:)