Meteor collection find() does not retrieve any data - javascript

I'm working through a meteor tutorial : https://www.meteor.com/tutorials/blaze/collections
I have a collection defined,
Tasks = new Mongo.Collection("tasks");
I've added two items to it, one directly from meteor mongo command line, and another one using:
Tasks.insert({ text: "Testing JS", createdAt: new Date() });
Here are the results from running db.tasks.find() on the backend:
{ "_id" : ObjectId("559e9569abbb64fe1d5fd89a"), "text" : "Hello world!", "createdAt" : ISODate("2015-07-09T15:38:17.742Z") }
{ "_id" : "obRN8Rcssa9yJqXzA", "text" : "Testing JS", "createdAt" : ISODate("2015-07-09T17:00:13.285Z") }
But when I run Tasks.find({}); on the front end, I get empty result. It just gives me a long JSON but no data from the database.

In meteor, you can view the documents returned by a cursor by calling fetch on it. For example:
console.log(Tasks.find().fetch());

Looking at your code, you are not publishing anything:
if (Meteor.isServer) {
Meteor.publish("tasks", function () {
console.log(Tasks.find());
});
}
needs to be
if (Meteor.isServer) {
Meteor.publish("tasks", function () {
console.log(Tasks.find());
return Tasks.find();
});
}
or just remove it if you use autopublish.

Did you import the .js file of collection in front-end .js ?
import { Tasks } from '/your_path_goes_here/filename_goes_here.js';
If not import it and try below,
Add Publish like
if (Meteor.isServer) {
Meteor.publish('tasks', function task() {
return Task.find({});
});
}
Subscribe it in Front-End .js
Meteor.subscribe('task');

Related

How to get the children of an object in Firebase database?

I am trying to alert this array using Firebase Realtime Database:
"discuss" : {
"taj" : {
"20200809" : {
"comment" : "first comment",
"full_date" : "2020/08/09"
}
}
}
I tried to do something like this:
data.val().children.forEach(children => {
alert(children.comment)
});
But came out with nothing.
Does anybody have an idea?
If you want to get comment from the database, then try the following:
let ref = firebase.database().ref("dicuss");
ref.child("taj").on("value", function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var comment = childSnapshot.val().comment;
console.log(comment);
});
});
Add a reference to the node taj then iterate and retrieve the comment attribute.
Check the docs:
https://firebase.google.com/docs/database/web/read-and-write

Update database entry using mongoose

Hello i am using mongoose.
I have built this query that finds my desired project :
const projects = await ClientManagers.findOne({'project.contactPerson.work_email' : 'testing#email.com'} , { 'project.$.companyName': 1 });
this returns an object from my database like this :
{
'projectName' : 'x',
'companyName' : 'x bv'
}
How can i update the company name to be 'Y bv' instead of 'x bv'.
Assuming this is your document structure,
{
"_id" : ObjectId("5f2ae5a4b1549ac0460920dd"),
"projectName" : "A",
"project" : [
{
"companyName" : "T1",
"contactPerson" : {
"work_email" : "t1#gmail.com"
}
},
{
"companyName" : "T2",
"contactPerson" : {
"work_email" : "t2#gmail.com"
}
}
]
}
Single Update updateOne()
If you know email will be unique and want to update single document then use updateOne().
first is query part to find condition, email t1#gmail.com
second is set/update part, here $ is for array because project is an array, update companyName to T1 Company
await ClientManagers.updateOne(
{ 'project.contactPerson.work_email': 't1#gmail.com' },
{
$set: { "project.$.companyName": "T1 Companmy" }
}
)
Multiple Update updateMany()
If email is not unique and want to update everywhere then use updateMany(), it will update every matching documents.
await ClientManagers.updateMany(
{ 'project.contactPerson.work_email': 't1#gmail.com' },
{
$set: { "project.$.companyName": "T1 Company" }
}
)
Not suggesting update() method to use, because its deprecated in mongoose and will give Deprecation Warnings
, this function is replaced with updateOne(), updateMany() and replaceOne() methods.
Good start. Mongo has better documentation with examples. I suggest you to refer that also.
use update
db.collection.update({companyName:'x bv'}, {"$set":{"companyName":y}})
Mongo is case sensitive. So name should match exactly.
update updates one document. To update multiple, use updateMany or multi:true option with update or findOneAndMondify for one update for find and update case.

Only download Firebase snapshots that have child key

My data structure looks like this (removed unnecessary parts):
{
"threads" : {
"PUSHID" : {
"info" : {
"members" : {
"uid" : true,
"uid2" : true
}
}
}
}
}
I'm trying to write some javascript to pull snapshots of threads a user is in, but I can't figure out a way for it to work without pulling snapshots of each thread. This is my code now that pulls each thread snapshot.
firebase.database().ref('threads').on('child_added', function(snapshot) {
if (snapshot.hasChild('info/members/' + userUid)) {
// Display thread info
}
});
I tried to make a query with .orderByChild('info/members/' + userUid) and removing null snapshots, but I would have to add a .indexOn for each userUid which is obviously not practical.
Your current structure makes it easy/efficient to look up the users for a thread. But your use case is to look up the threads for a user. You'll need to augment your data model to allow the use-case:
{
"user_threads" : {
"uid": {
"PUSHID": true,
"PUSHID2": true
},
"uid2": {
"PUSHID": true,
"PUSHID3": true
}
}
}
And then read it with:
firebase.database().ref('user_threads/'+userUid).on('child_added', function(snapshot) {
...
});
Modifying/expanding your data model to match the use-cases of your app is quite common when using NoSQL databases.

How to get Alfresco webscript JSON response in JavaScript file

I'm working on alfresco maven sdk project.
So i have a repo webscript who return a json response template containinng the list of files contained in a specific folder (in my case it's: Espace%20racine/Dictionnaire%20de%20données/Dossier%20Workflow).
The template response.json.ftl is here:
{
"Files" : [
<#list folder.children as child>
<#if child.isDocument>
{
"name" : "${child.name}"
}
<#if child_has_next>,</#if>
</#if>
</#list>
]
}
My webscript works fine, and when i run it through the commande line or the browser, i get a response like:
{
"Files" : [
{
"name" : "test"
}
,
{
"name" : "test2"
}
,
{
"name" : "test3"
}
]
}
Now, i want to invoke my webscript from a javascript file who reside in the folder web of share, but i haven't idea to how achieve this. I searched on the net but i don't find a consistant example to how manage this.
It'll help me lot if anyone can tell me the way to achieve this or take me an example to how achieve this.
Below is the code for making an ajax call in alfresco share.There are other methods also available like Alfresco.util.Ajax.jsonPost,Alfresco.util.Ajax.jsonPut etc..In below console.log(response.json) will print the response in console.
var fnSuccess = function RESPONSE_functionName(response)
{
console.log(response.json)//Here you will get response json.
};
var fnFailure = function RESPONSE_functionName(response)
{
Alfresco.util.PopupManager.displayMessage({text:"Failure"});
};
Alfresco.util.Ajax.jsonGet(
{
url: Alfresco.constants.PROXY_URI + "REPOSITORY_WEBSCRIPT_URL",
successCallback:
{
fn: fnSuccess,
scope: this
},
failureCallback:
{
fn: fnFailure,
scope: this
}
});

Meteor React Subscribe to specific collection data

I am building a React - Meteor web app and I need to get access to a specific piece of data from a collection.
There are three main components, the company list, the project list and the task list. When I list all of the companies, I can select one and then display all of the project associated with that company. What I want to then do is click on a project and see all of the tasks which are associated with that project. My data structure is as follows (as you can see, projects are an array of objects):
{
"_id" : "aQnrkqMi6ugEvav4a",
"owner" : "7Gp49ZCtGC9oEx3jN",
"createdAt" : ISODate("2017-05-08T15:52:27.777Z"),
"data" : {
"name" : "lkhgb",
"contacts" : [
{
...
}
],
"projects" : [
{
...
},
{
"name" : "ljhvljhvblhkjvblhkj",
"id" : "258757206",
"tasks" : [
"task1",
"task2"
]
}
]
}
}
In my TaskList Component, I'm exporting it like so:
export default createContainer((props) => {
const {companyId} = props.match.params;
Meteor.subscribe('company');
return {project: Company.findOne(companyId)};
}, TaskList)
Where I'm pulling the companyId off the props. I am also pulling off the projectId from props, but when I query the collection it's just returning undefined. I have published the GitHub repo as live here - https://github.com/GlueDigiStu/ClientManager and would appreciate any help.
You did indeed subscribe to the "company" publication on the server.
Although that publication does not exist.
Meteor.publish('company', function (companyId) {
const publications = [];
publications.push(Company.find(
{
_id: companyId,
},
));
return publications;
});
This will publish the id you want from the server.
Then the subscription from the client would be:
Meteor.subscribe('company', props.companyId);
Let me know if you have any further questions.
If I may I'll also give you some suggestions. Having documents in documents in documents isn't really great for publications.
I would suggest to place projects and tasks into new collections and only subscribe to tasks once their project is selected.

Categories

Resources