Meteor - move data from one collection to another - javascript

I'm trying to move Mongo document from one collection to another and I can't get it to work (in server method):
var oldData = newCollection.findOne({name: name});
if(oldData){
console.log(oldData); // this works
oldCollection.insert({oldData}); // this doesn't
}
Another way:
var oldData = newCollection.findOne({name: name});
if(oldData){
console.log(oldData.score); // this works
oldCollection.insert({
score: oldData.score
}); // this doesn't
What's wrong here?

You shouldn't need the curly brackets in option 1 - oldCollection.insert(oldData)

Related

Copy multiple repetition PID-3 to PID-4

I have an ORU interface in Mirth which splits to two destinations. I need to make some changes to the PID in Mirth before sending to one destination which I have managed except I cannot seem to copy all of PID3 to PID 4 just the first repetition.
Mirth Connect: 3.7.1
Transformer Code:
var i = msg['PID']['PID.3'].length();
var assigner = msg['PID']['PID.3'][0]['PID.3.4']['PID.3.4.1'].toString();
// PID tweaking for xxx
while(i--)
{
//Copy all of PID-3 to PID-4
msg['PID']['PID.4']['PID.4.1']=msg['PID']['PID.3'][i]['PID.3.1'].toString()
msg['PID']['PID.4']['PID.4.4']['PID.4.4.1']=msg['PID']['PID.3'][i]['PID.3.4']
['PID.3.4.1'].toString()
msg['PID']['PID.4']['PID.4.5']=msg['PID']['PID.3'][i]['PID.3.5'].toString()
msg['PID']['PID.4']['PID.4.6']=msg['PID']['PID.3'][i]['PID.3.6'].toString()
if (msg['PID']['PID.3'][i]['PID.3.5'].toString() == '016') {
// Copy MRN into PID-2
msg['PID']['PID.2']['PID.2.1']=msg['PID']['PID.3'][i]['PID.3.1'].toString();
}
//Delete PID-3 and replace with DUMMY ID
if (i!=0){
delete msg['PID']['PID.3'][i];
} else{
msg['PID']['PID.3'][i]['PID.3.1']='DUMMY ID';
delete msg['PID']['PID.3'][i]['PID.3.2'];
delete msg['PID']['PID.3'][i]['PID.3.3'];
delete msg['PID']['PID.3'][i]['PID.3.4'];
delete msg['PID']['PID.3'][i]['PID.3.5'];
delete msg['PID']['PID.3'][i]['PID.3.6'];
}
}
Raw PID:
PID|||485286^^^MRN&&GUID^016^MRN~2858365^^^AUID&&GUID^004^AUID||
Transformed PID:
PID||485286|DUMMY ID|485286^^^MRN^016^MRN|
Desired Transformed PID:
PID||485286|DUMMY ID|485286^^^MRN^016^MRN~2858365^^^AUID&&GUID^004^AUID|
You need to index your left hand side. For example, instead of
msg['PID']['PID.4']['PID.4.5']=msg['PID']['PID.3'][i]['PID.3.5'].toString()
You would need
msg['PID']['PID.4'][i]['PID.4.5']=msg['PID']['PID.3'][i]['PID.3.5'].toString()
Thanks Gavin, I did initially try this but got the error:
TypeError: Cannot set property "PID.4.1" of undefined to "2858365"
After some more investigation I realised that I needed to create the repetitions in PID-4.
So I addition to what Gavin mentioned I needed to add the following above:
//Ensure a PID.4 exists for each PID.3 repetition
var i = msg['PID']['PID.3'].length()-1;
while(i--) {
msg['PID']['PID.4']=msg['PID']['PID.4']+<PID.4/>;
}
var i = msg['PID']['PID.3'].length();
There is an official repository of code templates at https://github.com/nextgenhealthcare/connect-examples
There is a useful code template for doing this called renameField found here.
Using that code template, you can reduce all of your code down to
// Copy all repetitions of PID-3 to PID-4
msg['PID']['PID.4'] = renameField(msg['PID']['PID.3'], 'PID.4');
// Iterate over PID-3 repetitions
for each (var pid3 in msg['PID']['PID.3']) {
if (pid3['PID.3.5'].toString() == '016') {
// Copy MRN into PID-2
msg['PID']['PID.2']['PID.2.1'] = pid3['PID.3.1'].toString();
}
}
// Replace all PID-3 with single repetition containing only DUMMY ID using xml literal
msg['PID']['PID.3'] = <PID.3><PID.3.1>DUMMY ID</PID.3.1></PID.3>;

mongodb / javascript variable in query

i have:
var optiontable = {1: 'attack', 2: 'defence', 3: 'intelligence'};
var option = 1;
var minion = minions[thisminion]; // get correct, works.
console.log(optiontable[option]); // would output "attack"
var nameoption = optiontable[option];
var increasement = 8;
how would i do to get the minion.attack based on:
thisminion.nameoption // this wont work when it should display the result of thisminion.attack
and get the nameoption to use in:
minions.update({
_id: thisminion._id,
userid: playerid
}, {$inc: {nameoption: increasement}})
Hard to tell without looking at the rest of the code, but looks like you just need to change
thisminion.nameoption
to
thisminion[nameoption]
Since your original line is trying to access thisminion's property called 'nameoption'. Using square brackets would access the property named the same as the value of nameoption.
As for the mongo part: since you can't use a variable as left-hand value, you need to do a little bit of extra work:
var updateObj = {};
updateObj[nameoption] = increasement;
then you can do this:
minions.update({
_id: thisminion._id,
userid: playerid
}, {$inc: updateObj})
Similar questions:
How to set mongo field from variable
Using variables in MongoDB update statement

How to delete a key within an object in local storage with Javascript and/or Jquery?

In local storage I have an object named favourites and it contains this..
"{
"id3333":{
"URL":"somewhere.comm/page1/",
"TITLE":"Page 1 Title",
},
"id4444":{
"URL":"somewhere.comm/page2/",
"TITLE":"Page 2 Title",
}
}"
How can I delete an object based on its ID (id3333 & id4444 for examples)
I have tried the following along with some other voodoo..
localStorage.removeItem('id3333'); // no errors, no removal
localStorage.removeItem('favourites':'id3333'); // SyntaxError: missing ) after argument list
localStorage.removeItem('favourites[id3333]'); // no errors, no removal
localStorage.removeItem('id3333', JSON.stringify('id3333')); // no errors, no removal
Also, I will need to get the key name to delete based on a variable, so like this..
var postID = 'id3333';
localStorage.removeItem(postID);
or
var objectName = 'favourites';
var postID = 'id3333';
localStorage.removeItem(objectName[postID]);
Is it possible to remove a nested item directly or do I need to retrieve the full object and then delete the item and then set the object back to local storage again?
The closest I can get to deleting anything directly so far is..
localStorage.removeItem('favourites');
But that of course removes the entire object.
You have a a single key and you are acting like there are multiple keys
var obj = {
"id3333":{
"URL":"somewhere.comm/page1/",
"TITLE":"Page 1 Title",
},
"id4444":{
"URL":"somewhere.comm/page2/",
"TITLE":"Page 2 Title",
}
};
window.localStorage.favs = JSON.stringify(obj); //store object to local storage
console.log("before : ", window.localStorage.favs); //display it
var favs = JSON.parse(window.localStorage.favs || {}); //read and convert to object
var delKey = "id3333"; //key to remove
if (favs[delKey]) { //check if key exists
delete favs[delKey]; //remove the key from object
}
window.localStorage.favs = JSON.stringify(favs); //save it back
console.log("after : ", window.localStorage.favs); //display object with item removed
With localStorage.removeItem you can only remove top level keys, i.e. keys directly on localStorage.
Because id3333 is on localStorage.favourites you cannot remove it using localStorage.removeItem.
Instead try delete localStorage.favourties['id3333']
Simple, actually: you just delete it. :)
x = {
"id3333":{
"URL":"somewhere.comm/page1/",
"TITLE":"Page 1 Title",
},
"id4444":{
"URL":"somewhere.comm/page2/",
"TITLE":"Page 2 Title",
}
};
console.log(x);
delete x.id3333;
console.log(x);
delete does what you're looking for. You could also do something like delete x.id3333.TITLE if you were so inclined. Note also that delete returns true if successful and false if not.
Suppose you set a nested object in localStorage like that
const dataObj = {
uid: {
name: 'robin',
age: 24,
}
}
window.localStorage.setItem('users', JSON.stringify(dataObj));
Now you want to delete the age property. You can't remove it with removeItem native function since it allows to delete from top level.
So you need to get the data first and delete the property you want and set the data again to localStorage with updated value like that
const existingLocalStorage = JSON.parse(window.localStorage.getItem('users') || {});
if(existingLocalStorage['uid']['age']) { // if throws any error, use lodash get fucntion for getting value
delete existingLocalStorage['uid']['age'];
}
window.localStorage.setItem('users', JSON.stringify(existingLocalStorage));

Add [DataObject] to exsisting array with var key

Using Cordova, I am trying to get an Object to add to an array. I have this working on node JS using :
theData = {[varkey]:DataObject};
But I can't get this to work the same way within my javascript that cordova runs.
I need to do the following:
var TownName = 'Auckland', var townData = (JSON Data);
theArray = new Array();
theArray[TownName] = townData;
I need to be able to call it back as:
theArray['Auckland']
Which will return (JSON Data)
But it doesn't want to store the data with the key inside the array.
I have also tried:
theArray.TownName = townData;
theArray = [{TownName:townData}];
theArray = {[TownName]:townData}];
Nothing wants to store the data.
Any suggestions?
::EDIT::
data.theData =
"Auckland"[
{
"username":"pndemoname1",
"number":"373456",
"www":"http://373456.pndemoname1",
"icon":"/imgs/pndemoname1.png"
},
{
"username":"pndemoname2",
"number":"373458",
"www":"http://373458.pndemoname2",
"icon":"/imgs/pndemoname2.png"
}
data.town = "Auckland";
townData = new Array();
alert(JSON.stringify(data.theData))//Alerts theData
townData[data.town] = data.theData
alert(townData[townName]) //Alerts undefined
::EDIT2::
Re-defining the array within the function that deals with all of the data, seems to make it work.
As per my answer, the issue was that I assumed javascript vars are global.
Use objects or an array of objects.
A data structure like this:
{
town1: town1Data,
town2: town2Data,
}
Or more common:
[
{
name: "Town 1",
data: {...}
},
{
name: "Town 2",
data: {...}
},
]
For reference:
http://andrewdupont.net/2006/05/18/javascript-associative-arrays-considered-harmful/
I got what you're trying to do, to add property names dynamically to your object is first, by making sure you are using an OBJECT instead of an array, so when you want to store something you will do the following:
var _obj = {}, _something = 'xyz';
_obj[ _something ] = { ... }; // json structure
The problem you're facing is that you want to assign a string value as a key inside your array, which will not work.
However, you can still use the array you defined and do the following:
var _array = new array();
_array.push( { .... } ); // insert json structure
Remember! By using the array you will have to loop through all values every time you want to access your key, just as the best practice to avoid getting into errors.
Good luck.
The issue was that I didn't define the array within the function of where I was trying to add the information to.
I assumed the var was global (Too much PHP)

Ways to pass values into Javascript options objects using ASP

Options objects (so called) are used to collect static parameters from the page for a javascript file to operate on. What are the best ways to pass dynamic values from the page into a javascript options object?
Eg how should you insert a value for MyAlbumID in the following
MyOptionsObject({
flashvars: {
xmlFilePath: "http://myurl.com/images.php?album=" + MyAlbumID
})​
Where MyAlbumID is obtained from:
var albumspan = document.getElementById("lblMyAlbum");
var albumtextnode = albumspan.firstChild;
var MyAlbumID = albumtextnode.data;
What exactly is the problem?
onDynamicAction(function(){
// ensure that the DOM is loaded when that action happens
var myAlbumID = document.getElementById("lblMyAlbum").firstChild.data;
// lets hope there is an element with that id and a textnode,
// so that this threw no error
MyOptionsObject ({
flashvars: {
xmlFilePath: "http://myurl.com/images.php?album=" + myAlbumID
} // missing bracket, btw.
});
// set a new object
})

Categories

Resources