Passing Data of Page 1 to Page 3 in ionic 3 - ionic-framework

How can I pass the data of home(page 1) to ContactPage(page 3) along with DetailsPage(page 2) data?
lets say
HomePage has data of "UserId"
DetailsPage has data of "UserName" & "UserAddress" //this page doesnt have UserID
now how can i get the data of
"UserId" as well as "UserName & UserAddress" on ContactPage?

in HomePage pass data as follow
this.navCtrl.push(DetailsPage, {
UserId: 'UserId'
});
in about page
--retrieve value using
this.UserId = navParams.get('UserId');
--and send data from DetailsPage to ContactPage as
this.navController.push(ContactPage, {
UserId: this.UserId, UserName: 'UserName', UserAddress: 'UserAddress'
});

You can follow several ways
From your home page store userId in localStorage
localStorage.setItem('userId',<your variable>)
then from your second page if it is not a root page
this.navCtrl.push('ContactPage',{
userName:<your variable>,
userAddress:<your variable>
})
then from your third page
fetch userId from localStorage like localStorage.getItem('userId') and the userName and userAddress using NavParams
this.navParams.get('userName'),
this.navParams.get('userAddress '),
let me know your outcome

Refer following link to pass data from one page to another page
Passing-data-between-pages-in-an-ionic-application

Related

populate or aggregate 2 collection with sorting and pagination

I just use mongoose recently and a bit confused how to sort and paginate it.
let say I make some project like twitter and I had 3 schema. first is user second is post and third is post_detail. user schema contains data that user had, post is more like fb status or twitter tweet that we can reply it, post_detail is like the replies of the post
user
var userSchema = mongoose.Schema({
username: {
type: String
},
full_name: {
type: String
},
age: {
type: Number
}
});
post
var postDetailSchema = mongoose.Schema({
message: {
type: String
},
created_by: {
type: String
}
total_reply: {
type: Number
}
});
post_detail
var postDetailSchema = mongoose.Schema({
post_id: {
type: String
}
message: {
type: String
},
created_by: {
type: String
}
});
the relation is user._id = post.created_by, user._id = post_detail.created_by, post_detail.post_id = post._id
say user A make 1 post and 1000 other users comment on that posts, how can we sort the comment by the username of user? user can change the data(full_name, age in this case) so I cant put the data on the post_detail because the data can change dynamically or I just put it on the post_detail and if user change data I just change the post_detail too? but if I do that I need to change many rows because if the same users comment 100 posts then that data need to be changed too.
the problem is how to sort it, I think if I can sort it I can paginate it too. or in this case I should just use rdbms instead of nosql?
thanks anyway, really appreciate the help and guidance :))
Welcome to MongoDB.
If you want to do it in the way you describe, just don't go for Mongo.
You are designing the schema based on relations and not in documents.
Your design requires to do joins and this does not work well in mongo because there is not an easy/fast way of doing this.
First, I would not create a separate entity for the post details but embedded in the Post document the post details as a list.
Regarding your question:
or I just put it on the post_detail and if user change data I just
change the post_detail too?
Yes, that is what you should do. If you want to be able to sort the documents by the userName you should denormalize it and include in the post_details.
If I had to design the schema, it would be something like this:
{
"message": "blabl",
"authorId" : "userId12",
"total_reply" : 100,
"replies" : [
{
"message" : "okk",
"authorId" : "66234",
"authorName" : "Alberto Rodriguez"
},
{
"message" : "test",
"authorId" : "1231",
"authorName" : "Fina Lopez"
}
]
}
With this schema and using the aggregation framework, you can sort the comments by username.
If you don't like this approach, I rather would go for an RDBMS as you mentioned.

Search for user with Mongoose

User's have accounts and can comment using a Comment Mongoose model.
Is it possible to allow a user to enter a username in an text input and generate all user associated comments?
I've been trying to use db.users.find({"name") but I'm not sure how to pass a username from an input field to search. Any suggestions?
Thanks! The comment model is below. It connects each comment with a user.
var commentSchema = new mongoose.Schema({
body: String,
author: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
username: String
}
})
You have to take the user name entered by another user from the text box and send it with the request to back end, something like {uName : 'user2'}.
Use the value from the object coming with the request and do find in your data base.
Something like below will be the backend code:-
var uName = req.params.uName
var cursor = db.users.find({username : uName.toString()});
Now cursor will give you record from user2 from your data base.
Front-end
send your GET request with Query String to the Back-end API
<form method="get" action="/comments/user" >
<input type="text" name="username" placeholder="username">
<button type="submit">Search</button>
</form>
Back-end
write a API to deal with GET request and Mongoose Query
var app = express();
var Comment = mongoose.model('Comment', commentSchema);
app.get('/comments/user', function (req, res) {
var query=Comment.find();
//get the Query String here
var filter=req.params.username;
if(filter.length>0){
query.where({author.username:filter});
}
query.exec(function (error, comment) {
//send the result back to front-end
res.json({ Comment: comment });
});
});

How to subscribe to certain records in collection with id in meteor

I'm new to meteor,I have two collections named Employee and Visitors.and two templates Home and Print. i can get the id of the record from the button like this.
printVisitor:function(){
return Visitor.findOne({_id:Session.get('UpdateVisitorId')});
}
now when i click button that redirects to another page and i need to print those values say name, phone number using the particular id of record which i could get from the above code.
my route code looks like this
Router.route('/print/:_id', {
name: 'print',
controller: 'PrintController',
action: 'action',
where: 'client'
});
and my print html is this
<template name="Print">
This is: {{VisitorName}}
Visiting:{{EmployeeName}}
Phone:{{PhoneNumber}}
how can i publish and subscribe and print those certain values of that id
Publish and subscribe can take arguments. You also want to use this.params to get the URL parameters. This is the general pattern
Router.route('/foo/:_id', {
name: 'foo',
waitOn: function(){
// this grabs the ID from the URL
var someId = this.params._id;
// this subscribes to myPublication while sending in the ID as a parameter to the publication function
return [
Meteor.subscribe('myPublication', someId)
]
}
});
/server/publications.js
Meteor.publish('myPublication', function(id){
check(id, String);
return MyCollection.findOne({_id: id});
});
You'll now have access to the data you subscribed to on this route.

How to show data from mongoDB with ObjectID

i have an "back end" application which write in MongoDb (in database i have _id: with ObjectId("13f6ea...002")) i use meteor app to show information. Everything was good i displays list of information with {{#each}}. But when i wanted show one element with '_Id' nothing works.
I read this issue and adapt my code to get right root, But i can't display anything on the page. I tried to write Template helpers but it didn't helped
Db record:
{
_id: ObjectId("13f6ea...002"),
url: "foo",
title: "bar",
published: "2014-08-22 03:26:21 UTC",
image: "foo.jpg",
summary: "foo ",
categories: [
"F",
"B"
],
...
}
Route:
this.route('news', {
path: '/news/:_id',
template: 'news',
waitOn: function () {
var id = this._id;
Meteor.subscribe('news', id);
},
data: function() {
var id = this.params._id;
return News.findOne({ _id: Meteor.Collection.ObjectID(this.params._id)});
},
action : function () {this.render();},
});
Publish
Meteor.publish('news', function(id) {
return News.find({_id: id});
});
Template which redirect to unique post
<h4>{{title}}</h4>
And template is just {{news}}
How can i fix this?
UPDATE
My solutions to fix that:
router.js
waitOn: function () {
var id = this._id;
Meteor.subscribe('News', id);
},
data: function() {
return News.findOne(new Meteor.Collection.ObjectID(this.params._id));
},
and in template
<a href="news/{{_id._str}}">
Navigate to the appropriate url in your browser (i.e. localhost:3000/news/[_id]), open the console and enter:
Router.current().data()
That will show you the data context of the current route. Either it returns nothing, in which case there is a fundamental problem with your News.findOne query as it's returning nothing, or (more likely) it returns the required document.
In the latter case, as far as I can see there is no news property within that document, which is why it isn't rendering anything. If you change {{news}} to {{url}} or {{summary}} I would imagine it would render the requested property.
If by {{news}} you're trying to render the entire document, then (aside from the fact that it will render as something like [Object]) you need to make news a property of the object returned by your data function:
return {
news: News.findOne({ _id: Meteor.Collection.ObjectID(this.params._id)});
};
Getting Document with _id :
In the .js file under events, Say on click event and Collection EventList :-
'Submit form' : function () {
var id = this._id;
return EventList.find({_id : id}).fetch();
}
This would return the object for the id. In my Case, I am displaying a field for all documents in Collection. User selects a record and clicks Submit, which fetches all Document fields and displays to the User

Twitter Typeahead data passing

I'm using twitter typeahead bundle. I have code like this:
var results = new bloodhound({...})
//results is a map in json with the keys ID and displayName. It's grabbed by ajax call.
$('#element').typeahead({
...
{
name: 'Results',
displayKey: 'displayName',
source: results.ttAdapter(),
}...
This works very, very well. The results are being populated as users type stuff. However, one issue I have is that when the user clicks "submit", the value I need to grab is not the displayName, but instead the ID.
If the user types, "Exam", let's say the results map looks like: {"id":"100","displayName":"Example1"}. They click on "Example1" and example1 jumps to the text box. However, when the user clicks submit, I want "100" to come from that element, rather than "Example1". Is that possible?
You could use the typeahead selected handler
.on("typeahead:selected", function(event, item) {
$("#hiddenInputToSubmit").val(item.id);
return;
}
Maybe that could work for you?