I am running into some difficulty exporting a table to csv in meteor/blaze. I am following: [http://rafaelquintanilha.com/export-your-json-data-to-csv-format/][1].
I have a Template.event that is triggering the export button
Template.export.onCreated( () => {
Template.instance().subscribe('table');
});
Template.export.helpers({
exportContacts() {
return Contacts.find();
}
});
Template.export.events({
'click .export-data' () {
MyAppExporter.exportAllContacts();
}
});
it is calling exportAllContacts() in a global helper
MyAppExporter = {
exportAllContacts: function() {
var self = this;
Meteor.call("exportContacts", function(error, data) {
if ( error ) {
alert(error);
return false;
}
var csv = Papa.unparse(data);
self._downloadCSV(csv);
});
},
_downloadCSV: function(csv) {
var blob = new Blob([csv]);
var a = window.document.createElement("a");
a.href = window.URL.createObjectURL(blob, {type: "text/plain"});
a.download = "contacts.csv";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
}
and the helper is calling a Meteor.method exportContacts
Meteor.methods({
exportContacts: function() {
let fields = [
"Email",
“Some Contact",
"Created Date",
"Hard Bounce",
"Unsubscribed"
];
let data = [];
let contacts = Contacts.find().fetch();
for(let i = 0; i < contacts.length; i++) {
let contact = contacts[i];
let contactString = JSON.stringify(contact);
_.each(contactString, function(c) {
console.log("Inside Loop", contactString);
data.push([
c.contact.emailAddress,
c.contact.someContact,
c.contact.creationDate,
c.contact.hardBounceBack,
c.contact.unsubscribed
]);
console.log("DATA", data)
return {fields: fields, data: data};
});
}
}
});
I keep getting an error that “emailAddress is not defined exportContacts.js:20:17
20160426-22:00:47.957(-4)? Inside Loop {"_id":"dRnXRdZrbR9CYdmBx","contact":[{"emailAddress":"fred#weasly.com","someContact":"No","creationDate":"N/A","hardBounceBack":"N/A","unsubscribed":"N/A"}]}
I20160426-22:00:48.029(-4)? Exception while invoking method 'exportContacts' ReferenceError: emailAddress is not defined
I20160426-22:00:48.029(-4)? at server/methods/exportContacts.js:20:17
I20160426-22:00:48.029(-4)? at Function._.each._.forEach (packages/underscore.js:142:22)
I20160426-22:00:48.029(-4)? at _loop (server/methods/exportContacts.js:17:7)
but I cannot seem to figure out how to access the contacts. I am logging it out (see above in logs). Any help would be appreciated.
ADDED LOGS
let contacts = Contacts.find().fetch(); console.log(contacts)
I20160427-09:06:23.484(-4)? CONTACTS [ { _id: 'dRnXRdZrbR9CYdmBx', contact: [ [Object] ] },
I20160427-09:06:23.484(-4)? { _id: 'LHmW4R9PLM5D7cZxr', contact: [ [Object] ] },
I20160427-09:06:23.484(-4)? { _id: 'jBdqQXz2b8itXJowX', contact: [ [Object] ] },
I20160427-09:06:23.484(-4)? { _id: 'bnDvNGX3i879z4wr2', contact: [ [Object] ] } ]
c.contact[0].emailAddress logged out
I20160427-09:22:08.142(-4)? Inside Loop {"_id":"dRnXRdZrbR9CYdmBx","contact":[{"emailAddress":"fred#weasly.com","someContact":"No","creationDate":"N/A","hardBounceBack":"N/A","unsubscribed":"N/A"}]}
I20160427-09:22:08.217(-4)? Exception while invoking method 'exportContacts' TypeError: Cannot read property '0' of undefined
I20160427-09:22:08.217(-4)? at server/methods/exportContacts.js:21:7
I20160427-09:22:08.217(-4)? at Function._.each._.forEach (packages/underscore.js:142:22)
I20160427-09:22:08.217(-4)? at _loop (server/methods/exportContacts.js:18:7)
I20160427-09:22:08.218(-4)? at [object Object].exportContacts (server/methods/exportContacts.js:15:46)
Within the _.each loop you are accessing the wrong data items. You can also use a _.each loop instead of the outer for loop too. If you do :
let contacts = Contacts.find().fetch();
_.each(contacts, function(contact) {
_each(contact.contact, function(c) {
data.push(
{
"email": c.emailAddress,
"contact": c. someContact,
"creationDate" : c.creationDate,
"bounceBack": c.hardBounceBack,
"unsubscribed": c.unsubscribed
}
})
})
This should solve your problem. This way you are looping through the outer contacts that is coming back from the fetch and then looping through the contact array from each element. This should be the most direct way to get down to the data you are looking for.
Your problem is this line: _.each(contactString, function(c) {
It should read: _.each(contact, function(c) {
:)
Related
I have made a scheduled script which is sending PDF though email.send()
I have get the filters as params from Suitelet. I want to get the name of the user (from runtime.getCurrentUser) and pass it to my PDF. I m just confused how to pass them and will that API be used in Suitelet or Sched script.
Can anyone help me with the code?
Here is my Scheduled script code:
/**
* #NApiVersion 2.x
* #NScriptType scheduledscript
*/
define(['N/ui/serverWidget', 'N/search', 'N/render', 'N/runtime', 'N/file', 'N/email'],
function (ui, search, render, runtime, file, email) {
function execute() {
try {
generateReport();
}
catch (e) {
log.error('generateReport ERROR', e);
}
}
function generateReport() {
var slfilters = runtime.getCurrentScript().getParameter({ name: 'custscript_searchfilter_report' });
log.debug('slfilters', slfilters);
if (!!slfilters) {
slfilters = JSON.parse(slfilters);
}
log.debug('slfilters2', slfilters);
var user = runtime.getCurrentUser();//Need this user to be passed to my xml template
var gender = slfilters.gender;//getting this from Suitelet
log.debug('gender', gender);
var item = slfilters.item;//getting this from Suitelet
log.debug('item', item);
var item_ = getItems(item, gender);
log.debug('getItems(item, gender)', item_);
//return item;
var xmlTemplateFile = file.load(3918);
//var template = script.getParameter({ name: 'custscript_template' });
var renderer = render.create();
renderer.templateContent = xmlTemplateFile.getContents();
var customSources = {
alias: 'searchdata',
format: render.DataSource.JSON,
data: JSON.stringify({
value: item_,
})
};
renderer.addCustomDataSource(customSources);
var xml = renderer.renderAsString();
var pdf = render.xmlToPdf({
"xmlString": xml
});
email.send({
author: 317,
recipients: 'aniswtf#gmail.com',
subject: 'Item Report',
body: 'Report Generated: ',
attachments: [pdf]
});
}
//
// ─── GET RESULTS ───────────────────────────────────────────────────
//
const getResults = function (set) {
var results = [];
var i = 0;
while (true) {
var result = set.getRange({
"start": i,
"end": i + 1000
});
if (!result) break;
results = results.concat(result);
if (result.length < 1000) break;
i += 1000;
}
return results;
};
//
// ─── GET ITEMS ───────────────────────────────────────────────────
//
function getItems(item, gender,user) {
try {
log.error('getItems Function started');
var itemSearch = search.load({
id: 'customsearch_mx_itemsearch'
});
var defaultFilters = itemSearch.filters;
itemSearch.filters.push(
search.createFilter({
name: "custitem5",
operator: 'anyof',
values: gender
}),
search.createFilter({
name: "internalid",
operator: 'anyof',
values: item
})
);
//defaultFilters = arrFilters;
//defaultFilters = defaultFilters.concat(arrFilters);
//log.error('Updated Filters', defaultFilters)
log.error('itemSearch', itemSearch);
//return defaultFilters;
var results = itemSearch.run().getRange({
start: 0,
end: 150
});
var result2 = results.map(function (x) {
// var results = getResults(itemSearch.run()).map(function (x) {
return {
'category': x.getText({
name: "custitem10",
join: "parent"
}),
'season': x.getValue({
name: "custitem11",
join: "parent"
}),
'riselabel': x.getText({
name: "custitem_itemriselabel",
join: "parent"
}),
'fit': x.getText({
name: "custitem9",
join: "parent"
}),
'name': x.getText({ //sku
name: "itemid",
join: "parent"
}),
'style': x.getText({
name: "custitem8",
join: "parent"
}),
'inseam': x.getText({
name: "custitem7",
join: "parent"
}),
'wash': x.getText({
name: "custitem_washname",
join: "parent"
}),
};
});
log.debug('Results', results.length);
log.debug('results', results);
log.debug('result2', result2);
// return results;//nabeeel's
return result2;//mine
} catch (e) {
log.error('error in getItems', e)
}
}
return {
execute: execute
};
});
There is no User in a Scheduled Script, so runtime.getCurrentUser() there will not return a value. You will need to retrieve the User via that method in the Suitelet (assuming it is not an anonymous external Suitelet).
From there you can add a Script Parameter to the Scheduled Script to hold the User, and then your Scheduled Script can read the Parameter and add the value as another Data Source on your template.
I'm writing a GraphQL query that will return mongoose data:
export const getInventory = () => {
let query = {
deletedAt: null
};
let stocks = await StockModel.find(query)
.sort({ material: 1 });
// Now separate each inventory item per material
let inventory = {};
stocks.map(stock => {
if (inventory[stock.material]) {
inventory[stock.material].quantity += stock.quantity;
} else {
let clone = Object.assign({}, stock);
clone.lot = null;
clone.expirationDateTime = null;
clone.partNumber = null;
clone.manufacturer = null;
inventory[stock.material] = clone;
}
});
let list = Object.keys(inventory).map(key => {
return inventory[key];
});
return list;
};
Here is my inventory GraphQL query:
const inventory = {
type: new GraphQLList(StockType),
description: "Get all inventory",
resolve(root, args, context) {
return getInventory();
}
};
When running the following query:
{
viewer {
inventory() {
id
quantity
lot
}
}
}
I'm getting the following GraphQL error:
{
"errors": [
{
"message": "Expected value of type \"Stock\" but got: [object Object].",
"locations": [
{
"line": 3,
"column": 5
}
],
"stack": "Expected value of type \"Stock\" but got: [object Object].\n\nGraphQL request (3:5)\n2: viewer {\n3: inventory {\n ^\n4: id\n\n at invalidReturnTypeError (/dev/node_modules/graphql/execution/execute.js:766:10)\n at completeObjectValue (/dev/node_modules/graphql/execution/execute.js:758:13)\n at completeValue (/dev/node_modules/graphql/execution/execute.js:660:12)\n at completeValueWithLocatedError (/dev/node_modules/graphql/execution/execute.js:580:21)\n at completeValueCatchingError (/dev/node_modules/graphql/execution/execute.js:556:21)\n at /dev/node_modules/graphql/execution/execute.js:684:25\n at Array.forEach (<anonymous>)\n at forEach (/dev/node_modules/iterall/index.js:83:25)\n at completeListValue (/dev/node_modules/graphql/execution/execute.js:680:24)\n at completeValue (/dev/node_modules/graphql/execution/execute.js:643:12)\n at /dev/node_modules/graphql/execution/execute.js:617:14\n at process._tickCallback (internal/process/next_tick.js:68:7)",
"path": [
"viewer",
"inventory",
0
]
}
}
How can I solve the error?
I am trying to add an object to an array but it is not working with me, the program can't read the property push
I defined an array in <script>:
Data: function() {
return {
Projects: [
{
name: '',
id: 0,
subscribers: 0,
products: {name:'',color:''},
}
],
}
And in the function:
GetAllWorkspaces: function(){
var app = this;
const instance = axios.create({
timeout: 1000,
headers: {
........
}
});
instance.get("XXXXXXX")
.then( function(response) {
console.log(response);
Object.keys(response.data.result).forEach( function (product) {
var subscribersCounter = 0;
let example = {
name: response.data.result[product].name,
id: response.data.result[product].id,
subscribers: response.data.result[product].subscribers,
products: response.data.result[product].products,
};
let uploadedExample = {
name: '',
id: '',
subscribers: '',
products: {name:'',color:''},
};
uploadedExample.name = example.name;
uploadedExample.id = example.id;
if ( example.subscribers ) {
Object.keys(example.subscribers).forEach(function (key) {
subscribersCounter++;
});
}
uploadedExample.subscribers = subscribersCounter;
if ( example.products ) {
Object.keys(example.products).forEach(function (Pkeys) {
uploadedExample.products.name = Pkeys;
Object.keys(example.products[Pkeys]).forEach(function (key) {
if (key == 'color') {
uploadedExample.products.color = example.products[Pkeys][key];
}
});
});
}
//add the new workspace to the list of workspaces.
app.Projects.push(uploadedExample);
});
})
.catch(function(error) {
console.log(error);
});
My problem is with this line
app.Projects.push(uploadedExample);
where when I try to push an object into the array, the error message is shown:
TypeError: Cannot read property 'push' of undefined
As the error says, the problem is that app.Projects is undefined. This happens because 'this' refers to the function scope inside GetAllWorkspaces and not to the component scope (you can try it by console.logging 'this' - anyway- it is a good practice under all circumstances because 'this' can change from context to context). If you want to keep the component scope inside the method, you should use an arrow function like this:
GetAllWorkspaces: () => {
// do all your stuff
}
I have been stuck with this issues for 2 hours now and I really can't seem to get it work.
const app = new Vue({
el: '#book-search',
data: {
searchInput: 'a',
books: {},
},
methods: {
foo: function () {
axios.get('https://www.googleapis.com/books/v1/volumes', {
params: {
q: this.searchInput
}
})
.then(function (response) {
var items = response.data.items
for (i = 0; i < items.length; i++) {
var item = items[i].volumeInfo;
Vue.set(this.books[i], 'title', item.title);
}
})
.catch(function (error) {
console.log(error);
});
}
}
});
When I initiate search and the API call I want the values to be passed to data so the final structure looks similar to the one below.
data: {
searchInput: '',
books: {
"0": {
title: "Book 1"
},
"1": {
title: "Book 2"
}
},
Currently I get Cannot read property '0' of undefined.
Problem lies here:
Vue.set(this.books[i], 'title', item.title);
You are inside the callback context and the value of this is not the Vue object as you might expect it to be. One way to solve this is to save the value of this beforehand and use it in the callback function.
Also instead of using Vue.set(), try updating the books object directly.
const app = new Vue({
el: '#book-search',
data: {
searchInput: 'a',
books: {},
},
methods: {
foo: function () {
var self = this;
//--^^^^^^^^^^^^ Save this
axios.get('https://www.googleapis.com/books/v1/volumes', {
params: {
q: self.searchInput
//-^^^^--- use self instead of this
}
})
.then(function (response) {
var items = response.data.items
var books = {};
for (i = 0; i < items.length; i++) {
var item = items[i].volumeInfo;
books[i] = { 'title' : item.title };
}
self.books = books;
})
.catch(function (error) {
console.log(error);
});
}
}
});
Or if you want to use Vue.set() then use this:
Vue.set(self.books, i, {
'title': item.title
});
Hope this helps.
yep, the problem is about context. "this" returns not what you expect it to return.
you can use
let self = this;
or you can use bind
function(){this.method}.bind(this);
the second method is better.
Also google something like "how to define context in js", "bind call apply js" - it will help you to understand what is going wrong.
// update component's data with some object's fields
// bad idea, use at your own risk
Object
.keys(patch)
.forEach(key => this.$data[key] = patch[key])
i have an object like this :
var data = {
scope: "some_scope",
redirect_urls: {
return_url: return_url,
cancel_url: cancel_url
},
someotherobj:{
method: "get"
},
setRedirectUrls: function(return_url, cancel_url) {
this.redirect_urls.return_url = return_url;
this.redirect_urls.cancel_url = cancel_url;
}
}
and i want to have this object JSON.strigify'ed but i miss something or i dont know if this is the right way like :
data.setRedirectUrls('http://www...','http://www....');
var json = JSON.stringify(data);
but i get ReferenceError: "return_url" is not defined.
The problem is you're trying to instantiate your objects properties with variables which haven't been defined/don't exist, change them to empty strings.
var data = {
scope: "some_scope",
redirect_urls: {
return_url: "",
cancel_url: ""
},
someotherobj: {
method: "get"
},
setRedirectUrls: function(return_url, cancel_url) {
this.redirect_urls.return_url = return_url;
this.redirect_urls.cancel_url = cancel_url;
}
}
data.setRedirectUrls('http://www...', 'http://www....');
var json = JSON.stringify(data);
console.log(json)
.as-console-wrapper {max-height: 100% !important;top: 0;}
Hey Check out my snippet, I added an explanation to my answer there.
var data = {
scope: "some_scope",
redirect_urls: {
return_url: '', // return_url: return_url; Can't do this here because they don't exist.
cancel_url: '' // cancel_url: cancel_url;
},
someotherobj:{
method: "get"
}
}
// After you know the structure of your object then add the actions to it here.
data.setRedirectUrls = function(return_url, cancel_url) {
this.redirect_urls.return_url = return_url;
this.redirect_urls.cancel_url = cancel_url;
}
data.setRedirectUrls('http://www...','http://www....');
var json = JSON.stringify(data);
console.log(json);