I have a mongo document called New which has a sub document called hashtags which has th value name which is a string. I am trying to add all the hashtags belongs to a new in a variable but is adding extra characters (seems that the name string value is not well decoded from the bson or something like that).
New document:
var newSchema = new Schema({
...
hashtags: [{
type : mongoose.Schema.ObjectId,
ref: 'Hashtag'
}]
});
Hashtag document:
var mongoose = require('mongoose')
var Schema = mongoose.Schema
var hashtagSchema = new Schema({
color: {
type: String,
default: '#000000'
},
name: {
type: String
}
});
var hashtag = mongoose.model('Hashtag', hashtagSchema )
module.exports = hashtag
Snniped testing code:
docs.forEach(noticia => {
if(noticia.hashtags.length > 0){
for(i in noticia.hashtags){
if(noticia.hashtags[i] && noticia.hashtags[i].name){
text += '#' + noticia.hashtags[i].name.replace(/\s/g,'') + ' '
}
}
}
})
console.log(text)
Console output:
#Lula #toBSON #_cast #_markModified #_registerAtomic #$__getAtomics #hasAtomics #_mapCast #push #nonAtomicPush #$pop #pop #$shift #shift #pull #splice #unshift #sort #addToSet #set #toObject #inspect #indexOf #pull
I have tried to apply noticia.hashtags[i].name.replace(/\s/g,'').toString() :
docs.forEach(noticia => {
if(noticia.hashtags.length > 0){
for(i in noticia.hashtags){
if(noticia.hashtags[i] && noticia.hashtags[i].name){
text += noticia.hashtags[i].name.toString() + ' '
}
}
}
})
Console output
LulatoBSON_cast_markModified_registerAtomic$__getAtomicshasAtomics_mapCastpushnonAtomicPush$poppop$shiftshiftpullspliceunshiftsortaddToSetsettoObjectinspectindexOfpull
How I have to decode this string value?
The for...in statement iterates over all non-Symbol, enumerable properties of an object.
var string1 = "";
var object1 = {a: 1, b: 2, c: 3};
for (var property1 in object1) {
string1 += object1[property1];
}
console.log(string1);
// expected output: "123"
You are iterating over the properties of noticia.hashtags
Related
I want to create a cloud function in firebase that parses csv files and adds them as a firestore document. Somehow, none of the libraries I try seem to work. Whenever I add a csv library, the function won't even deploy. therefore, I want to parse a CSV file without using a library. However, I cant seem to figure out how to parse it correctly when using maps and arrays. I have the following string in string type. Replaced some data with placeholders.
subject,contacts,dateStart,dateEnd,timeStart,timeEnd,isRecurring,frequence,location,room,visitors,sendInvite
Sollicitatie,[{"name":"<name here>","email":"<email here>","phoneNumber":"<phone number here>"}],28-09-2020,28-09-2020,13:00,14:00,true,yearly,<location here>,2,[{"name":"<name here>","email":"<email here>","visitorImage":"imgRef","visitorType":"Relation","phoneNumber":"<telephone here>","licencePlate":"<licencePlate>"}],true
I want to get this data in firestore as the following:
subject: subject,
contacts:[
{
name: name,
email: email,
phoneNumber: phoneNumber
},
{
name: name,
email: email,
phoneNumber
}
],
dateStart: dateStart,
dateEnd, dateEnd,
timeStart, timeEnd,
isRecurring, isRecurring,
frequence, frequence,
location: location,
room: room,
visitors:[
{
name: name,
email: email,
phoneNumber: phoneNumber
},
{
name: name,
email: email,
phoneNumber
//Whatever data also needs to be added
}
]
Is there a library that will work/is there a problem with my package.json, or is there a script that will work? I tried to write a script that will do this, but it is far from working. I have tried the following and got the right strings, but I cant get the arrays the right way.
const convert = (from, to) => str => Buffer.from(str, from).toString(to);
const hexToUtf8 = convert('hex', 'utf8');
const string = hexToUtf8(contents);
const data = string.split('\n');
for(i=0;i<=data.length - 1;i++){
if(i == 0){
continue;
}
const row = data[i];
const rowValues = [];
const elems = row.split(",");
console.log(elems);
var arrayParse;
var parsing = 0;
for(j=0;j<=elems.length - 1;j++){
const elem = elems[j];
if(parsing == 1){
arrayParse += "," + elem;
continue;
}else if(elem[0] == "["){
arrayParse += elem;
parsing = 1;
continue;
}else if(elem[elem.length - 1] == "]"){
arrayParse += elem;
parsing = 0;
rowValues.push(arrayParse);
arrayParse = "";
continue;
}else{
rowValues.push(elem);
}
}
console.log(rowValues);
I have a Suitelet that calls a sublist but I would like to trigger a filter when an "addButton" is clicked.
Suitelet:
var form = serverWidget.createForm({ title : 'Unbilled Orders', hideNavBar : false });
form.addField({id: 'name_criteria', label: 'Name', type: serverWidget.FieldType.MULTISELECT, source: 'customer'});
form.addButton({label: 'Filter',id: 'custpage_mybutton',functionName: 'myButtonFunction()'});
var name_field = context.request.parameters.name_criteria;
//# Filter does not work as name_field='' #
var objSublistSearch = search.load({ id: SEARCH_ID });
var filterArray = objSublistSearch.filters;
filterArray.push(search.createFilter({ name: 'entity', operator: search.Operator.ANYOF, values: name_field }));
objSublistSearch.filters = filterArray;
var SublistSearch = objSublistSearch.run();
...
context.response.writePage(form);
Client script (does not update the sublist):
function myButtonFunction() {
// Load current record in order to manipulate it
var objRecord = currentRecord.get()
var field2 = objRecord.getValue({
fieldId: 'name_criteria',
});
log.debug("field2",field2 );}
You can use currentRecord's insertLine and removeLine methods to update the sublist but please note that they will only work for editable sublists (INLINEEDITOR and EDITOR).
If you are using SublistType.LIST, you will have to reload the suitelet in your myButtonFunction().
You're only looking at the "body" field not a sublist.
Try using sublist functions within the client script.
var lines = objRecord.getLineCount({sublistId:'custpage_sublistid'});
for (line = 0; line < lines; line++) {
objRecord.selectLine({sublistId:'custpage_sublistid', line:line });
var existingValue = objRecord.getSublistValue({sublistId:'custpage_sublistid', fieldId:'custpage_columnid', line: line });
objRecord.setCurrentSublistValue({sublistId:'custpage_sublistid', fieldId:'custpage_columnid', value: 12345 });
// do other stuff
}
in my MEAN app, I have a situation, where I'm reading in some csv data line by line, and using 'upsert' to update the existing records, as well as to insert those, which do not match the 'CSCOpportunityID' field value:
if(metaFields.subjectCategory === 'SalesforceData'){
//Here we will use the upsert logic to either insert new, or update existing records
for (var i = 0; i < result.length; i++) {
//console.log(result[i]);
var inserted = 0;
for (var i = 0; i < result.length; i++) {
var dataRecord = result[i];
dataRecord.OriginalDocumentName = fileName;
dataRecord.DocumentAuthor = metaFields.documentAuthor;
dataRecord.TabName = tabName;
dataRecord.SubjectCategory = metaFields.subjectCategory;
dataRecord.Subject = metaFields.subject;
dataRecord.DateDocumentProduced = metaFields.dateDocumentProduced;
dataRecord.DateDocumentReceived = metaFields.dateDocumentReceived;
dataRecord.DocumentSubmitter = metaFields.documentSubmitter;
dataRecord.DocumentReviewer = metaFields.documentReviewer;
dataRecord.OriginalSource = metadataFields.originalSource,
dataRecord.DataVersion = metadataFields.dataVersion,
dataRecord.DataFields = newCsvLines[0];
collection.update(
{
CSCOpportunityId: dataRecord.CSCOpportunityID
},
{
$set: {
OriginalDocumentName: dataRecord.OriginalDocumentName,
DocumentAuthor: dataRecord.DocumentAuthor,
TabName: dataRecord.TabName,
SubjectCategory: dataRecord.SubjectCategory,
Subject: dataRecord.Subject,
DateDocumentProduced: dataRecord.DateDocumentProduced,
DateDocumentReceived: dataRecord.DateDocumentReceived,
DocumentSubmitter: dataRecord.DocumentSubmitter,
DocumentReviewer: dataRecord.DocumentReviewer,
OriginalSource: dataRecord.OriginalSource,
DataVersion: dataRecord.DataVersion,
DataFields: dataRecord.DataFields,
CSCOpportunityID : dataRecord.CSCOpportunityID,
OpportunityName: dataRecord.OpportunityName ,
AccountName : dataRecord.AccountName,
OpportunityOwner : dataRecord.OpportunityOwner,
Stage : dataRecord.Stage,
Industry : dataRecord.Industry,
ACV : dataRecord.ACV,
RevenueStartDate : dataRecord.RevenueStartDate,
RevenueTerm : dataRecord.RevenueTerm,
ProbabilityPct : dataRecord.ProbabilityPct,
DealRegion : dataRecord.DealRegion
}
},
{upsert: true},
{multi:true}
//if (++inserted == result.length) {
// console.log("finished");
// //db.close();
//}
);
}
}
}
The update parts works, but the insert part does not, because I do not know how to make an insert, when the value of 'CSCOpportunityID' finds no match.
What is the proper way of handlding this?
The update method takes 4th argument as callback function. Since you provided an object {multi: true}, it won't make any db call, only return a promise. Correct syntax is:
collection.update(query, fieldsToSet, {upsert: true, multi: true}, function(err,doc){
})
Here's what I want to achieve in mongodb but as a javascript example.
var array = [];
//Initiating an array
for(var i = 0; i < 30; i++) {
array[i] = 0;
}
//Changing a value at an index
array[14] = 1;
//Getting a value at an index
console.log(array[4]);
//Swapping Two Variables.
var temp = array[14];
array[14] = array[12];
array[12] = temp;
So far I can set a schema
var schema = new mongoose.Schema({
array: {Number: Number, Value: Number}
});
I can Initiate it later on
schema.statics.name = function(cb) {
var new = new Array({
{Number: 1, Value: 0},
{Number: 2, Value: 0},
...
{Number: 30, Value: 0}
});
new.save();
I can loop through them all
c.user.array.forEach(function (element) {
console.log(element.Number);
console.log(element.Value);
})
But I get stuck with finding a single value or setting/switching them.
It seems overly complicated for something so simple in a programming language; ive been trying for the last few hours and there so many {} and $'s that its making my head hurt.
From what I can find I should be using .find() but then there no examples of how the stuff in the schema is laid out.
Schema:
array: [{value: Number}]
Can be initialised as many times as you want:
backpack: [{value: 0}, {value:0}, {value:0}]
and can be accessed like
.array[1].value
more info on sub docs and children
Thanks to Tomalak
It seems as if my embedded documents are not being saved to their respective collections. Here's my model:
var County = new Schema({
_id : Schema.ObjectId,
name : String,
biggestCity : String
});
var Country = new Schema({
_id : Schema.ObjectId,
name : String,
counties : {type: [County], ref: "County"}
});
var Continent = new Schema({
_id : Schema.ObjectId,
countries : {type: [Country], ref: "Country"},
});
...and here's the code that I use to save them to MongoDB:
var continentModel = mongoose.model("Continent");
var continent = new continentModel();
country.name = name;
var countryModel = mongoose.model("Country");
var countyModel = mongoose.model("County");
for (var i = 0; i < req.body.countries.length; i++) {
var country = new countryModel();
country.name = req.body.countries[i].name;
for (var j = 0; j < req.body.countries[i].counties.length; j++) {
var county = new countyModel();
county.name = req.body.countries[i].counties[j].name;
county.biggestCity = req.body.countries[i].counties[j].biggestCity;
countries.counties.push(county);
}
continent.countries.push(country;
}
continent.save();
If I do a db.continents.find(), a document comes back with all the properties (including country and county) populated.
But if I do a db.counties.find() or a db.countries.find(), nothing comes back. So it seems as if the County and Country documents are not being saved to the DB to their respective collections, but rather saved to the Continent collection as regular properties instead (not embedded documents).
What am I doing wrong?
This may be too simple, but you are only calling continent.save() and never calling county.save() or country.save() at the end of the for loops. Is that just an omission or does that fix the problem. If it is an omission, please see my note about posting the output.