这是我的第一个gridfs-stream工作。我正在尝试从我的本地目录(C:\ work \ node \ test.txt)读取文件并将其写入gridfs-stream。当我运行此文件时,我收到以下错误
C:\ work \ node>节点测试 events.js:85 扔掉//未处理的'错误'事件 ^ 错误:ENOENT,打开'C:\ work \ node \ work ode est.txt' 在错误(本机)
我也试过使用try-catch但是也有同样的错误。谁能看到我犯的错误并指出?感谢。
这是我的代码..
var fs = require("fs"),
mongo = require("mongodb"),
Grid = require("gridfs-stream"),
gridfs,
writeStream,
readStream,
buffer = "";
mongo.MongoClient.connect("mongodb://localhost:27017/test", function (err, db) {
// "use strict";
gridfs = Grid(db, mongo);
// write file
writeStream = gridfs.createWriteStream({ filename: "test.txt" });
fs.createReadStream("C:\work\node\test.txt").pipe(writeStream);
});
答案 0 :(得分:0)
现在可以使用更正的文件路径
var fs = require("fs"),
mongo = require("mongodb"),
Grid = require("gridfs-stream"),
gridfs,
writeStream,
readStream,
buffer = "";
mongo.MongoClient.connect("mongodb://localhost:27017/test", function (err, db) {
// "use strict";
gridfs = Grid(db, mongo);
// write file
writeStream = gridfs.createWriteStream({ filename: "test.txt" });
**fs.createReadStream("C:\/work\/node\/test.txt").pipe(writeStream);**
});