节点fsmkdirSync错误

时间:2016-01-13 16:07:57

标签: javascript node.js fs

我有以下文件夹结构:

enter image description here

CacheModule.js内部我有以下代码:

    socket.on('saveUserCache', function (obj, user_id) {
    var jsonOutput = JSON.stringify(obj);
    if (!fs.existsSync('./cacheObjects/' + user_id)) {
        fs.mkdirSync('./cacheObjects/' + user_id, 0777, function (err) {
            if (err) {
                console.log(err);
            }
        });
    }
    fs.writeFile('./cacheObjects/' + user_id + '/cache.json', jsonOutput, function (err) {
        if (err) throw err;
        console.log('It\'s saved!');
    });
});

这背后的想法是检查用户文件夹是否存在(如果不存在)创建新文件夹然后写入文件。

但是我收到以下错误:

Error: ENOENT: no such file or directory, mkdir './cacheObjects/125'
at Error (native)
at Object.fs.mkdirSync (fs.js:794:18)
at Socket.<anonymous> (/var/www/project/app_server/costum_modules/CacheModule.js:10:16)
at emitTwo (events.js:87:13)
at Socket.emit (events.js:172:7)
at Socket.onevent (/var/www/project/app_server/node_modules/socket.io/lib/socket.js:330:8)
at Socket.onpacket (/var/www/project/app_server/node_modules/socket.io/lib/socket.js:290:12)
at Client.ondecoded (/var/www/project/app_server/node_modules/socket.io/lib/client.js:193:14)
at Decoder.Emitter.emit (/var/www/project/app_server/node_modules/socket.io/node_modules/socket.io-parser/node_modules/component-emitter/index.js:134:20)
at Decoder.add (/var/www/project/app_server/node_modules/socket.io/node_modules/socket.io-parser/index.js:247:12)
at Client.ondata (/var/www/project/app_server/node_modules/socket.io/lib/client.js:175:18)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at Socket.onPacket (/var/www/project/app_server/node_modules/socket.io/node_modules/engine.io/lib/socket.js:99:14)
at emitOne (events.js:82:20)
at WebSocket.emit (events.js:169:7)

任何人都可以告诉我为什么会这样。它看起来像是用字符串发送mkdir。我也给了正确的权限(所以它不缺乏权限)

0 个答案:

没有答案