I'm using the IBM Cloudant NoSQL database, and I'm trying to upload a document to it in Node.js. The document (.xlsx) is first read in and converted to json:
xlsxj({
input: "testdata/WHO_NREVSS.xlsx",
output: "testdata/WHO_NREVSS.json"
}, function(err, result) {
if(err) {
console.error(err);
}else {
// Read the file and send to the callback
fs.readFile('./testdata/WHO_NREVSS.json', handleFile)
//The callback function
function handleFile(err, data) {
if (err) throw err
else{
//data = data.replace( "\"", '\\"');
obj = JSON.parse(data);
So far it works fine. When I console.log obj, I get an array full of json data:
[
{
"H3N2v": "0",
"REGION TYPE": "Census Regions",
"REGION": "New England",
"YEAR": "2007",
"WEEK": "40",
"timeline": "2007-40",
"TOTAL SPECIMENS": "115",
"PERCENT POSITIVE": "0",
"A (H1)": "0",
"A (Unable to Subtype)": "0",
"A (H3)": "0",
"2009 H1N1": "0",
"A (Subtyping not Performed)": "0",
"B": "0"
},...
]
Now, when I want to insert this array in a document on Cloudant, which looks like this...:
testdb.insert({"_id": "statistic", "data":obj}, function(error, result){
if(error) {
console.error(error);
}else {
console.log(JSON.stringify(obj));
}
})
... I just get a document with "_id" and "_rev" as a result, but the key "data" and the value "obj" don't appear at all. Why is that the case? When I manually (by copy - paste) insert the array in cloudant, it works fine.
Update 1: When using "data" from the callback function handleFile, I actually get a result in the database, but it looks like this:
"data": [
91,
123,
34,
72,
51,
78,
...
]
While it should not. Logging it gives me the result "Buffer 5b 7b 22 48 33 ... "
SOLUTION
The problem was solved by moving the whole 'insert' part in the callback function. Now it works the way it should work. The final code:
fs.readFile('./testdata/WHO_NREVSS.json', handleFile)
// Write the callback function
function handleFile(err, data) {
if (err) throw err
else{
obj = JSON.parse(data);
testdb.insert({"_id": "statistic", "data":obj}, function(error, result){
if(error) {
console.error(error);
}else {
console.log(obj);
}
})