Answered by:
How to return file content with readTextAsync ?

Question
-
I am reading a file content wirh readTextAscyn method and it works well. The problem is that i need to work with this content and push it to a variable named sampleGroups. But how to return this variable in order to work with it outstide my readTextAsync method ?
Here is my code :
Windows.Storage.KnownFolders.picturesLibrary.getFileAsync(workshopsListFile).done( function (file) { Windows.Storage.FileIO.readTextAsync(file).done( function (fileContent) { // Split file into array var workshops = fileContent.split(";"); // Getting workshops array lentgh var workshopsLentgh = workshops.length; // Initializing sampleGroups array sampleGroups.length = 0; for (var i = 0; i < (workshopsLentgh - 1) ; i++) sampleGroups[i] = "{ key: group" + [i + 1] + ", title:" + workshops[i] + ", subtitle: " + status[i] + ", description: " + groupDescription + "},"; }, function (error) { // Generating toast notification if the file is unreadable var messagedialogpopup = new Windows.UI.Popups.MessageDialog("Unable to read the sources file", 'Error'); messagedialogpopup.showAsync(); }); }, function (error) { // Generating toast notification is the is not available on the repository var messagedialogpopup = new Windows.UI.Popups.MessageDialog("The specific file was not found on the directory", 'Error'); messagedialogpopup.showAsync(); });
I need to work with my sampleGroups variable after this code, just as follow :
var sampleItems = [ { group: sampleGroups[0], title: "Status", subtitle: "Active", description: itemDescription, content: group1ItemStorageContent }, { group: sampleGroups[0], title: "Description", subtitle: "More informations", description: itemDescription, content: itemContent }, { group: sampleGroups[0], title: "Environments", subtitle: "11", description: itemDescription, content: itemContent }, ];
- Edited by Shnabr Wednesday, April 2, 2014 11:25 AM
Wednesday, April 2, 2014 11:21 AM
Answers
-
You did not declare "sampleGroups" as an array. You can declare it by using var sampleGroups = []; or var sampleGroups = new Array(); . Also what I usually do is use the push method to push the information into the array, but you can try your way of using assignments.
- Proposed as answer by datQkiD2 Sunday, April 6, 2014 5:25 PM
- Marked as answer by Matt SmallMicrosoft employee, Moderator Friday, April 11, 2014 3:20 PM
Sunday, April 6, 2014 5:25 PM
All replies
-
I don't understand what's wrong with the code you have.
Matt Small - Microsoft Escalation Engineer - Forum Moderator
If my reply answers your question, please mark this post as answered.
NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.Wednesday, April 2, 2014 1:17 PMModerator -
This code is working but I would like to return the variable sampleGroups in order to use it after the readtextasync function. How can i do ?Wednesday, April 2, 2014 1:26 PM
-
I must be missing something. Aren't you assigning it here?
sampleGroups.length = 0; for (var i = 0; i < (workshopsLentgh - 1) ; i++) sampleGroups[i] = "{ key: group" + [i + 1] + ", title:" + workshops[i] + ", subtitle: " + status[i] + ", description: " + groupDescription + "},";
Matt Small - Microsoft Escalation Engineer - Forum Moderator
If my reply answers your question, please mark this post as answered.
NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.Wednesday, April 2, 2014 1:52 PMModerator -
I do but when i try to use this variable after that, it gives me an error (the variable is not recognized). Here is the entire code :
function generateSampleData() { var itemContent = "<p>Some text</p>"; var group1ItemStorageContent = "Test"; var itemDescription = "Some text"; var groupDescription = "Some text"; var workshopsListFile = "File.txt"; var sampleGroups; // Reading workshopsListFile content // Getting file with asyncrhonous method Windows.Storage.KnownFolders.picturesLibrary.getFileAsync(workshopsListFile).done( function (file) { Windows.Storage.FileIO.readTextAsync(file).done( function (fileContent) { // Split file into array var workshops = fileContent.split(";"); // Getting workshops array lentgh var workshopsLentgh = workshops.length; // Initializing sampleGroups array sampleGroups.length = 0; for (var i = 0; i < (workshopsLentgh - 1) ; i++) sampleGroups[i] = "{ key: group" + [i + 1] + ", title:" + workshops[i] + ", subtitle: " + status[i] + ", description: " + groupDescription + "},"; }, function (error) { // Generating toast notification if the file is unreadable var messagedialogpopup = new Windows.UI.Popups.MessageDialog("Unable to read sources file", 'Error'); messagedialogpopup.showAsync(); }); }, function (error) { // Generating toast notification is the is not available on the repository var messagedialogpopup = new Windows.UI.Popups.MessageDialog("The specific file was not found on the directory", 'Error'); messagedialogpopup.showAsync(); }); var messagedialogpopup = new Windows.UI.Popups.MessageDialog(sampleGroups , 'Content'); messagedialogpopup.showAsync(); }
- Edited by Shnabr Wednesday, April 2, 2014 3:32 PM
Wednesday, April 2, 2014 3:14 PM -
Can you provide a working sample? Post it to OneDrive.
Matt Small - Microsoft Escalation Engineer - Forum Moderator
If my reply answers your question, please mark this post as answered.
NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.Thursday, April 3, 2014 2:04 PMModerator -
This looks like a scoping issue. sampleGroups is only available within generateSampleData. Where are you trying to use it? If you are trying to use it from another scope, you need to declare the variable outside generateSampleData.Thursday, April 3, 2014 6:07 PM
-
@Matt : I'm doing it, thank you for your help.
@Mike : Yes it is, but i'm just trying to use it in generateSampledata() in my sampleItems array.
(function () { "use strict"; var list = new WinJS.Binding.List(); var groupedItems = list.createGrouped( function groupKeySelector(item) { return item.group.key; }, function groupDataSelector(item) { return item.group; } ); // TODO: Replace the data with your real data. // You can add data from asynchronous sources whenever it becomes available. generateSampleData().forEach(function (item) { list.push(item); }); WinJS.Namespace.define("Data", { items: groupedItems, groups: groupedItems.groups, getItemReference: getItemReference, getItemsFromGroup: getItemsFromGroup, resolveGroupReference: resolveGroupReference, resolveItemReference: resolveItemReference }); // Get a reference for an item, using the group key and item title as acunique reference to the item that can be easily serialized. function getItemReference(item) { return [item.group.key, item.title]; } // This function returns a WinJS.Binding.List containing only the items that belong to the provided group. function getItemsFromGroup(group) { return list.createFiltered(function (item) { return item.group.key === group.key; }); } // Get the unique group corresponding to the provided group key. function resolveGroupReference(key) { return groupedItems.groups.getItemFromKey(key).data; } // Get a unique item from the provided string array, which should contain a group key and an item title. function resolveItemReference(reference) { for (var i = 0; i < groupedItems.length; i++) { var item = groupedItems.getAt(i); if (item.group.key === reference[0] && item.title === reference[1]) { return item; } } } // Returns an array of sample data that can be added to the application's data list. function generateSampleData() { var itemContent = "Some test"; var group1ItemStorageContent = "Test"; var itemDescription = "Some text"; var groupDescription = "Some description"; var workshopsListFile = "myFile.txt"; var sampleGroups; // Reading workshopsListFile content // Getting file with asyncrhonous method Windows.Storage.KnownFolders.picturesLibrary.getFileAsync(workshopsListFile).done( function (file) { Windows.Storage.FileIO.readTextAsync(file).done( function (fileContent) { // Split file into array var myVariable1 = fileContent.split(";"); // Getting workshops array lentgh var myVariableLentgh = myVariable1.length; // Initializing sampleGroups array sampleGroups.length = 0; for (var i = 0; i < (myVariableLentgh - 1) ; i++) sampleGroups[i] = "{ key: group" + [i + 1] + ", title:" + myVariable1[i] + ", subtitle: Some text, description: " + groupDescription + "},"; }, function (error) { var messagedialogpopup = new Windows.UI.Popups.MessageDialog("Unable to read the sources file", 'Error'); messagedialogpopup.showAsync(); }); }, function (error) { var messagedialogpopup = new Windows.UI.Popups.MessageDialog("The specific file was not found on the directory", 'Error'); messagedialogpopup.showAsync(); }); // Each of these sample items should have a reference to a particular group. var sampleItems = [ { group: sampleGroups[0], title: "Status", subtitle: "Active", description: itemDescription, content: group1ItemStorageContent }, { group: sampleGroups[0], title: "Description", subtitle: "More informations", description: itemDescription, content: itemContent }, { group: sampleGroups[0], title: "Environments", subtitle: "11", description: itemDescription, content: itemContent }, { group: sampleGroups[1], title: "Status", subtitle: "Active", description: itemDescription, content: itemContent }, { group: sampleGroups[1], title: "Description", subtitle: "More informations", description: itemDescription, content: itemContent }, { group: sampleGroups[1], title: "Environments", subtitle: "11", description: itemDescription, content: itemContent }, ]; return sampleItems; } })();
I'm facing this issue : JavaScript runtime error: Unable to get property '0' of undefined or null referenceThursday, April 3, 2014 6:21 PM -
Try this revision. You need to turn generateSampleData into an async function, and handle the result as a promise. I modified generateSampleData and the way that it is handled on returning. You may need to include additional code for handling the promise if it returns in an error state.
(function () { "use strict"; var list = new WinJS.Binding.List(); var groupedItems = list.createGrouped( function groupKeySelector(item) { return item.group.key; }, function groupDataSelector(item) { return item.group; } ); // TODO: Replace the data with your real data. // You can add data from asynchronous sources whenever it becomes available. //generateSampleData().forEach(function (item) { // list.push(item); //}); var prom = generateSampleData(); prom.done(function (result) { result.forEach(function (item) { list.push(item); }, function (error) { }); }); WinJS.Namespace.define("Data", { items: groupedItems, groups: groupedItems.groups, getItemReference: getItemReference, getItemsFromGroup: getItemsFromGroup, resolveGroupReference: resolveGroupReference, resolveItemReference: resolveItemReference }); // Get a reference for an item, using the group key and item title as acunique reference to the item that can be easily serialized. function getItemReference(item) { return [item.group.key, item.title]; } // This function returns a WinJS.Binding.List containing only the items that belong to the provided group. function getItemsFromGroup(group) { return list.createFiltered(function (item) { return item.group.key === group.key; }); } // Get the unique group corresponding to the provided group key. function resolveGroupReference(key) { return groupedItems.groups.getItemFromKey(key).data; } // Get a unique item from the provided string array, which should contain a group key and an item title. function resolveItemReference(reference) { for (var i = 0; i < groupedItems.length; i++) { var item = groupedItems.getAt(i); if (item.group.key === reference[0] && item.title === reference[1]) { return item; } } } // Returns an array of sample data that can be added to the application's data list. function generateSampleData() { var itemContent = "Some test"; var group1ItemStorageContent = "Test"; var itemDescription = "Some text"; var groupDescription = "Some description"; var workshopsListFile = "myFile.txt"; var sampleGroups = new Array(); // Reading workshopsListFile content // Getting file with asyncrhonous method return Windows.Storage.KnownFolders.picturesLibrary.getFileAsync(workshopsListFile).then( function (file) { return Windows.Storage.FileIO.readTextAsync(file).then( function (fileContent) { // Split file into array var myVariable1 = fileContent.split(";"); // Getting workshops array lentgh var myVariableLentgh = myVariable1.length; // Initializing sampleGroups array if (sampleGroups) { sampleGroups.length = 0; } for (var i = 0; i < (myVariableLentgh - 1) ; i++) sampleGroups[i] = "{ key: group" + [i + 1] + ", title:" + myVariable1[i] + ", subtitle: Some text, description: " + groupDescription + "},"; var sampleItems = [ { group: sampleGroups[0], title: "Status", subtitle: "Active", description: itemDescription, content: group1ItemStorageContent }, { group: sampleGroups[0], title: "Description", subtitle: "More informations", description: itemDescription, content: itemContent }, { group: sampleGroups[0], title: "Environments", subtitle: "11", description: itemDescription, content: itemContent }, { group: sampleGroups[1], title: "Status", subtitle: "Active", description: itemDescription, content: itemContent }, { group: sampleGroups[1], title: "Description", subtitle: "More informations", description: itemDescription, content: itemContent }, { group: sampleGroups[1], title: "Environments", subtitle: "11", description: itemDescription, content: itemContent }, ]; return WinJS.Promise.wrap(sampleItems); }, function (error) { var messagedialogpopup = new Windows.UI.Popups.MessageDialog("Unable to read the sources file", 'Error'); messagedialogpopup.showAsync(); return WinJS.Promise.wrap(null); }); }, function (error) { var messagedialogpopup = new Windows.UI.Popups.MessageDialog("The specific file was not found on the directory", 'Error'); messagedialogpopup.showAsync(); return WinJS.Promise.wrap(null); }); // Each of these sample items should have a reference to a particular group } })();
- Edited by Mike Jones (CSI)Microsoft employee Thursday, April 3, 2014 10:29 PM
Thursday, April 3, 2014 10:26 PM -
Hi Mike, thank you for your help. In fact i'm trying to load my listview dynamically with my file content. The code you gave me doesn't have any errors but was not able to load my listview. This last is appearing as empty on my screen and also when i try to show sampleItems content, it gives me an empty array.
I am sure that i'm handling this by the wrong way because i'm pretty new to Javascript.
Thursday, April 3, 2014 10:57 PM -
As a test, you may want to create a new Grid project and replace the data.js in the template with the above code. When I do that, the ListView loads with some of the sample data (no images). If you're not too far along in your coding, that might be a better starting point.
The items page still needs to be wired up with your item data.
Thursday, April 3, 2014 11:28 PM -
You did not declare "sampleGroups" as an array. You can declare it by using var sampleGroups = []; or var sampleGroups = new Array(); . Also what I usually do is use the push method to push the information into the array, but you can try your way of using assignments.
- Proposed as answer by datQkiD2 Sunday, April 6, 2014 5:25 PM
- Marked as answer by Matt SmallMicrosoft employee, Moderator Friday, April 11, 2014 3:20 PM
Sunday, April 6, 2014 5:25 PM