我是建一个索引文件,用来做好友的模糊查询。但是在批量写入索引文件的时候,就会出现各种问题。 例如
undefined:945
})
^
SyntaxError: Unexpected token }
at Object.Function (<anonymous>)
at /Volumes/macFile/nodeProject/cngouwo/routes/api/addFriend.js:21:23
at fs.js:266:14
at Object.oncomplete (fs.js:107:15)
Process finished with exit code 8
不仅仅这一个错误。各种各样的错误。 不知道为何如此?
代码奉上:
var fs = require('fs'),
util = require('util');
module.exports = function(req,res){
var id = req.body.id,
name = req.body.name,
friend = {};
fs.readFile('out.txt',function(err,data){
if(err){
var _collection = friend.collection = {},
_index = friend.index = {};
}
else{
var friends = data.toString();
friend = (new Function("return "+friends))();
var _collection = friend.collection,
_index = friend.index;
}
_collection['id_'+id] = name;
for(var x = 0 ; x < name.length ; x++){
if(!_index[name[x]])
_index[name[x]] = {};
if(!_index[name[x]]['id_'+id]){
_index[name[x]]['id_'+id] = {};
_index[name[x]]['id_'+id]['s'] = 1;
_index[name[x]]['id_'+id]['i'] = [x];
}else{
_index[name[x]]['id_'+id]['s'] += 1;
_index[name[x]]['id_'+id]['i'].push(x);
}
};
var json = util.inspect(friend,false,4);
fs.writeFile("out.txt", json, function(err) {
friend = null;
json = null;
if(err){
res.send('失败')
}else{
res.send({ms:'cd'})
}
});
});
}
8 回复