异步和等待功能 Node.js
async and await function Node.js
我已经按照this tutorial使用Node.js将文件上传到IPFS,但是我遇到了教程中没有出现的问题!
跟async和await函数有关,我不能这样说
const fileHash = fileAdded[0].hash;
我试过等待
const fileAdded = await ipfs.add({path: Name, content: file});
但不幸的是,我收到了一个错误(哈希未定义)。
我试过回调函数,但我不确定我的方式,因为 (fileAdded) 变量没有给我任何答案,而且它是未定义的,
这是完整代码:
const ipfsClient = require('ipfs-http-client');
const express = require('express');
const bodyParser = require('body-parser');
const fileUpload = require('express-fileupload');
const fs= require('fs');
const ipfs = new ipfsClient({host: 'localhost', port: '5001', protocol: 'http'});
const app= express();
var hash = require('hash');
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended:true}));
app.use(fileUpload());
app.get('/',(req,res)=>{
res.render('home');
});
app.post('/upload',(req,res)=>{
const f= req.files.file;
const Name = req.files.file.name;
const filePath ="files/"+ Name;
f.mv(filePath,async(err)=>{
if(err){
console.log("error failed to download a file");
return res.status(500).send(err);
}
const fileh = await addFile(Name,filePath);
fs.unlink(filePath, (err)=>{
if(err) console.log("error");
});
res.render('upload',{Name,fileh});
});
});
const addFile= async(Name,filePath)=> {
const file=fs.readFileSync(filePath);
const fileAdded = await ipfs.add({path: Name, content: file});
const fileHash = fileAdded[0].hash;
return fileHash;
};
app.listen(3000,()=>{
console.log("server is listen");
});
这是出现的错误:
我写了一个回调函数:
const addFile= async(Name,filePath)=> {
const file=fs.readFileSync(filePath);
const fileAdded = await ipfs.add({path: Name, content: file},(err,res)=>{
if(err)
console.log(err);
const fileHash = fileAdded[0].hash;
return fileHash;});};
但是 fileAdded 和 fileHash 的值未定义。
在我使用@Always Learning 中的这段代码后:
const addFile= async(Name,filePath)=> {
const file=fs.readFileSync(filePath);
const hashes = [];
const filesAdded = ipfs.add({path: Name, content: file});
for await (const result of filesAdded) {
hashes.push(result.hash);
console.log(result.hash);
console.log(result);
}
return hashes; // if you know it's just one for example
};
它给了我一个文件的信息,但是散列不起作用,因为它给了我未定义的,我只想提取这样的散列 "QmRndAYkvH3D2qhmYfaAZvWT6MDi4NiJPbzJor3EL87rrb"
因为 ipfs.add()
returns 一个异步可迭代对象,你需要使用 for async
来遍历它:
const addFile= async(Name,filePath)=> {
const file=fs.readFileSync(filePath);
const hashes = [];
const filesAdded = ipfs.add({path: Name, content: file});
for await (const result of filesAdded) {
if (result.hash) {
hashes.push(result.hash);
} else if (result.cid) {
hashes.push(result.cid.multihash); // guessing here - might be another part of the cid object
} else {
console.log("No hash found in",result);
}
}
return hashes[0]; // if you know it's just one for example
};
你必须改变这个:
const fileHash = fileAdded[0].hash;
进入这个:
const fileHash = fileAdded.hash;
(所以删除 [0]
)。
这对我有用:它与图书馆有关。
我已经按照this tutorial使用Node.js将文件上传到IPFS,但是我遇到了教程中没有出现的问题! 跟async和await函数有关,我不能这样说
const fileHash = fileAdded[0].hash;
我试过等待
const fileAdded = await ipfs.add({path: Name, content: file});
但不幸的是,我收到了一个错误(哈希未定义)。
我试过回调函数,但我不确定我的方式,因为 (fileAdded) 变量没有给我任何答案,而且它是未定义的,
这是完整代码:
const ipfsClient = require('ipfs-http-client');
const express = require('express');
const bodyParser = require('body-parser');
const fileUpload = require('express-fileupload');
const fs= require('fs');
const ipfs = new ipfsClient({host: 'localhost', port: '5001', protocol: 'http'});
const app= express();
var hash = require('hash');
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended:true}));
app.use(fileUpload());
app.get('/',(req,res)=>{
res.render('home');
});
app.post('/upload',(req,res)=>{
const f= req.files.file;
const Name = req.files.file.name;
const filePath ="files/"+ Name;
f.mv(filePath,async(err)=>{
if(err){
console.log("error failed to download a file");
return res.status(500).send(err);
}
const fileh = await addFile(Name,filePath);
fs.unlink(filePath, (err)=>{
if(err) console.log("error");
});
res.render('upload',{Name,fileh});
});
});
const addFile= async(Name,filePath)=> {
const file=fs.readFileSync(filePath);
const fileAdded = await ipfs.add({path: Name, content: file});
const fileHash = fileAdded[0].hash;
return fileHash;
};
app.listen(3000,()=>{
console.log("server is listen");
});
这是出现的错误:
const addFile= async(Name,filePath)=> {
const file=fs.readFileSync(filePath);
const fileAdded = await ipfs.add({path: Name, content: file},(err,res)=>{
if(err)
console.log(err);
const fileHash = fileAdded[0].hash;
return fileHash;});};
但是 fileAdded 和 fileHash 的值未定义。
在我使用@Always Learning 中的这段代码后:
const addFile= async(Name,filePath)=> {
const file=fs.readFileSync(filePath);
const hashes = [];
const filesAdded = ipfs.add({path: Name, content: file});
for await (const result of filesAdded) {
hashes.push(result.hash);
console.log(result.hash);
console.log(result);
}
return hashes; // if you know it's just one for example
};
它给了我一个文件的信息,但是散列不起作用,因为它给了我未定义的,我只想提取这样的散列 "QmRndAYkvH3D2qhmYfaAZvWT6MDi4NiJPbzJor3EL87rrb"
因为 ipfs.add()
returns 一个异步可迭代对象,你需要使用 for async
来遍历它:
const addFile= async(Name,filePath)=> {
const file=fs.readFileSync(filePath);
const hashes = [];
const filesAdded = ipfs.add({path: Name, content: file});
for await (const result of filesAdded) {
if (result.hash) {
hashes.push(result.hash);
} else if (result.cid) {
hashes.push(result.cid.multihash); // guessing here - might be another part of the cid object
} else {
console.log("No hash found in",result);
}
}
return hashes[0]; // if you know it's just one for example
};
你必须改变这个:
const fileHash = fileAdded[0].hash;
进入这个:
const fileHash = fileAdded.hash;
(所以删除 [0]
)。
这对我有用:它与图书馆有关。