Strange behavior with a link in my meteor app - meteor

I have something strange with a piece of code.
I have interventions and each intervention is associated to a customer. Then, in my interventions list template I use:
{{pathFor 'editCustomer' customer}} where customer is the customer _id.
If I print {{customer}} I get vFJHY2CtLi4GC7T5h but the link contains ueYXhWGL6mG3Cbq3v
ueYXhWGL6mG3Cbq3v corresponds to intervention _id
So the link is wrong beacause the id parameter is not that from the customer but the intervention
The document looks like:
{ "_id" : "ueYXhWGL6mG3Cbq3v", "title" : "intervention#1", "priority" : "medium", "customer" : "vFJHY2CtLi4GC7T5h", "assignedTo" : "97xzchCuSQGduz5vR", "issue" : "pokpokpok", "author" : "97xzchCuSQGduz5vR", "createdAt" : ISODate("2016-02-04T06:47:28.033Z") }
My router for customer id is:
Router.route('/customers/:_id', {
name: 'editCustomer',
data: function() {
return Customers.findOne(this.params._id);
}
});
An idea ?

Try to use
{{#with Customer}}
to set the data context just before
{{pathFor 'editCustomer' }}

The solution was to prefix the value with _id= like proposed by Łukasz Kapica
So: {{pathFor 'editCustomer' _id=customer}}
Thanks Łukasz Kapica

Related

Meteor query by array attribute

Suppose I have my collection as follows:
{
"_id" : "PipdmEzTMAziXjWBn",
"text" : "jkhkj",
"createdAt" : ISODate("2017-02-27T13:43:22.856Z"),
"hashtags" : [ "fwXJcu5CpKcYZpQ3v", "DCS4eLNiS7NjipiDQ" ] }
The important thing here is that i store id's of hashtags as an array.
Suppose I have a hashtag id as my input, how do I find all records that contain that id?
EDIT:
With the help of #zim, I did this:
Feeds.find({hashtags: {$in: "DCS4eLNiS7NjipiDQ"}});
But now it says:
Exception while simulating the effect of invoking 'feeds.findByHashtag' Error: $in needs an array
for a top-level query, you can use $in. e.g.
let hashtagId = ['abc123'];
collection.find({hashtags: {$in: hashtagId}});

How to do a lookup from a secondary collection in Meteor

I am building a simple chat client in Meteor. A chat message is stored in the Chats collection which has a message containing the id of the user from which it is and the id to which user it is, as well as the message, like so:
{
"_id" : "3Wo3EHYG8oPCS4TCc",
"message" :
{
"from" : "oSiKCdvCHGrfnfQoT",
"to" : "ESXbJXeWmNanz7zKq",
"text" : "Hello"
}
}
Users are store in the collection Users, which has the id as well as the username:
meteor:PRIMARY> db.users.find()
{ "_id" : "oSiKCdvCHGrfnfQoT", "username" : "user1" }
{ "_id" : "ESXbJXeWmNanz7zKq", "username" : "user2" }
I have an HTML-page that displays messages like this
<body>
<h1>Test</h1>
{{> hello}}
</body>
<template name="hello">
{{#each messages}}
From {{message.from}} to {{message.to}}: {{message.text}}
{{/each}}
</template>
using the following template helper:
Chats = new Mongo.Collection("chats");
Users = new Mongo.Collection("users");
import './main.html';
Template.hello.helpers({
messages:function(){
return Chats.find();
}
})
This creates the following output:
Test
From oSiKCdvCHGrfnfQoT to ESXbJXeWmNanz7zKq: Hello
However, in the HTML-page, I want to lookup the usernames of the users, instead of displaying their raw ids. I know that I could store the usernames in the Chats collection, but I would rather keep it clean. Of course, I could also do the lookup in the template helper function, and create a new datastructure, which mimics messages, but replaces the IDs with usernames. However, this is a rather clumsy solution, and I would rather do the lookup on the HTML-side. Is this possible?
Here's an approach that you can use all over your templates:
Template.registerHelper('username',function(userId){
var user = Meteor.users.findOne(userId);
return user && user.username;
});
Then in any template you can simply do {{username message.from}} (for example) and the username will automatically be inserted.

Removing the Last Item From a List created By .push()

I am having trouble deleting a single item from a list. I want to delete the 'oldest' item, and these have been added via the .push() method. It seemed pretty straightforward to do this but I am having issues. For my data structure, please see below. I am sure I am just doing something dumb as this must be a common use-case.
Any ideas/feedback would be greatly appreciated.
Code:
firebase.child('articlesList').orderByChild('site').equalTo('SciShow').limitToFirst(1).once('value', function(snapshot){
// This was one try, This seems to remove the entire articleList
snapshot.ref().remove();
// I have also tried this, and this seems to do nothing at all
snapshot.forEach(function(dataSnapshot){
dataSnapshot.ref().remove();
});
});
Data Structure:
"articlesList" : {
"-Jc16JziK668LV-Sno0s" : {
"id" : "http://gdata.youtube.com/feeds/api/videos/c8UpIJIVV4E",
"index" : "SciShow",
"link" : "http://www.youtube.com/watch?v=c8UpIJIVV4E&feature=youtube_gdata",
"site" : "SciShow",
"title" : "Why Isn't \"Zero G\" the Same as \"Zero Gravity\"?"
},
"-Jc16Jzkn6q41qzWw3DA" : {
"id" : "http://gdata.youtube.com/feeds/api/videos/Wi9i8ULtk4s",
"index" : "SciShow",
"link" : "http://www.youtube.com/watch?v=Wi9i8ULtk4s&feature=youtube_gdata",
"site" : "SciShow",
"title" : "The Truth About Asparagus and Your Pee"
},
"-Jc16Jzkn6q41qzWw3DB" : {
"id" : "http://gdata.youtube.com/feeds/api/videos/J7IvxfcOkmM",
"index" : "SciShow",
"link" : "http://www.youtube.com/watch?v=J7IvxfcOkmM&feature=youtube_gdata",
"site" : "SciShow",
"title" : "Hottest Year Ever, and Amazing Gecko-Man Getup!"
},
The folks over at Firebase answered this for me on their Google Group. I figured I would post for others to use.
= = =
Hey Ryan,
You are close! Instead of using the value event, you want to use the child_added event. The value event will get fired once with all the data at your /articlesList/ node. That is why you are seeing it delete the whole list. If you use the child_added event, it will fire for each child. Or, if you limit it like you did, it will only fire for a subset of children. One other thing to change is to use limitToLast(1) instead of limitToFirst(1) to get the last child.
Here's the code:
firebase.child('articlesList').orderByChild('site').equalTo('SciShow').limitToLast(1).once('child_added', function(snapshot){
snapshot.ref().remove();
});
Jacob

How do you find the PHID of a Phabricator object?

I need to get the PHIDs for one project and several users in our Phabricator install. It seems like it should be trivial to find out how to do this, but I've searched the docs to no avail. Am I looking in the wrong place or something?
Easiest way:
Go to the project
Click New Task
Look at the URL, it will have a parameter like:
?projects=PHID-PROJ-owipizovyry4fatifwfd
PHID is "PHID-PROJ-owipizovyry4fatifwfd"
Option 2:
Go to your Conduit [phabricator_url]\conduit
Find the method project.query
Enter the name in a JSON encoded array (i.e. ["project name"])
Click Call Method
PHID will be one of the data elements:
{
"data" : {
"PHID-PROJ-oybqquyhhke4awiw2akz" : {
"id" : "19",
"phid" : "PHID-PROJ-oybqquyhhke4awiw2akz",
"name" : "project name",
"members" : [
"PHID-USER-gapak5h34h6d5yvl67dx",
"PHID-USER-674vq754zfuhyxgvvq7x",
"PHID-USER-qvcdsyc4oz7rzpzziiyk",
"PHID-USER-qmefzjtsrmnxjxpc45km",
"PHID-USER-pbhygge7rgpdowz3s5vk"
],
"slugs" : [
"project_name"
],
"dateCreated" : "1396666703",
"dateModified" : "1396668261"
}
}
}
A more robust method would be to call the conduit method phid.lookup:
https://<your install>/conduit/method/phid.lookup/
Then enter in names something like #user, #project or Z2 and you'll get the PHID.

Struggling with .find() vs. .findOne() in my Meteor app

I realize that this might be very close to other posts, but I just can't get this to stick in my head! :( I need some help in trying to understand how to use .find() or should I be using .findOne()? (so confused) for a collection of mine.
Goal:
I want to get all of the documents out of the People collection and then for each document I want to create a new <option> where the .name is put in for text and the collection ._id is the value.
Here's some code:
The Collection results from Mongo
db.people.find()
{ "_id" : "1", "name" : "John" }
{ "_id" : "2", "name" : "Mike" }
{ "_id" : "3", "name" : "George" }
{ "_id" : "4", "name" : "Jane" }
My Template Helper :
Template.view_Admin_Staff.people = function() {
console.log( 'people : ', People.find() );
return People.find();
};
My Template :
<select id="ddStaffID" name="staff">
<option value="">-- Select One --</option>
{{#each people}}
<option value="{{_id}}">{{name}}</option>
{{/each}}
</select>
My console.log found in the Helper returns undefined. What in the world am I missing?
I am going to assume that your collection is People = new Meteor.Collectin('people'); And your template name for in the html is
When you did the console log in your template helper, you are logging the cursor itself, not the documents. You can find out more about cursor at (https://www.eventedmind.com/tracks/feed-archive/how-do-client-cursors-work).
To see are you returning the correct data, you could .fetch method on a cursor. Fetch will return an array of objects based on your query. In your case, the query is empty, the fetch method should return everything that is available in the client's db.
To answer your question in the title. both find and findOne are methods available on the Collection object.
find return a cursor. findOne return AN object, if there is a record matched to your query.
Bloody-h3ll! Warning to all us n00bs out there...one must subscribe to your publications in order for one to see and work with your publications. sigh
The code in my initial post is working as expected. I just simply forgot to subscribe to my exposing data in my publication in my client-side route. </foreheadSlap> I'm new enough to have doubted my query skills and didn't even thing to troubleshoot any further back in the code. Thank you #Bozhao and #DavidWeldon for your quick replies.

Resources