I can't get {{this.username}}
when I do:
{{#each history}}
{{this.username}} <!--THIS WILL NOT SHOW ANYTHING - OBJ in history->
{{this}} <!-- Shows data just fine, but cant get to any values like line above -->
{{/each}}
{{this}} <!-- THIS WILL SHOW ARRAY OF OBJECTS OK
{{history}} alone outputs this:
{ _id: 59fe71a6167aaa05f4dcf43c,
userid: 'asd',
name: 'asd',
email: 'asd#asd.com',
passwords: 'asd',
product: 'asd',
issues: 'asd',
serial: 'asd',
accessories: 'asdddddddd',
__v: 0 }
{ _id: 59fe8c8c49830e09d82de3f1,
userid: 'asd',
name: 'asd',
email: 'asd#gmagoaimg.com',
passwords: 'sdfgasdg',
product: 'afafafa',
issues: 'fasfasf',
serial: 'asdfa3a3fasfe',
backedup: 'on',
accessories: 'asdfgasdf',
__v: 0 }
{ _id: 59fe8f26703bf10a2a985f10,
userid: 'asd',
name: 'Homer Gomez',
email: 'ajsidfaos#faksdofa.com',
passwords: 'asdfasdf',
product: 'This is the product',
issues: 'This is the prod desc',
serial: 'afjdofajosdf98988',
backedup: 'on',
accessories: 'asdfasdf',
__v: 0 }
etc...
Now, my route sends over the data,
router.get('/service_history', function(req, res) {
//compares req['user'].name with the Job.userid if the same:
req['user'].service_history = []; // clears the user history
Job.find()
.then( function(doc){
//console.log(doc);
doc.forEach(function(job){
//if (err) throw err
if(job.userid == req['user'].name){
console.log(job);
req['user'].service_history.push(job);
}
});
});
res.render('service_history', { history: {data: req['user'].service_history} } );
});
: {{history}}
and when I render the page of this handlebars code, I can render the objects that I have pasted above in this post, above,
{{#each history}}
Thanks for any feedback, and sorry again for the newbish needs
Related
I'm New To firebase, i have a query
const posts = database.child('posts');
const user = database.child('user');
posts.orderByChild("id").on('child_added', snap => {
let user_id = snap.val().user_id;
let userRef = database.child('user/' + snap.val().user_id)
userRef.on('value', userSnap => {
console.log(userSnap.val().name)
let content = snap.val().content;
let date_posted = snap.val().date_posted;
let id = snap.val().id;
let title = snap.val().title;
let user_id = snap.val().user_id;
let user_Name = userSnap.val().name;
a.push({
"id": id,
"title": title,
"date_posted": date_posted,
"content": content,
"user_Name": user_Name,
"user_id": user_id
})
});
});
before entering UserRef Query, The posts are well sorted With The id,
however after the 2nd query, it's no longer sorted, How Can i make them sorted even after the 2nd query
this is the db structure
!:
i want the output to be like this
{
id: 1,
title: 'My First Post',
date_posted: '2018-05-02 19:40:02',
content: 'This is my firstpost!',
user_Name: 'Abdelrahman',
user_id: '8721da2c-0028-430f-a995-0d03c8abb393'
},
{
id: 2,
title: 'My SecondPost',
date_posted: '2018-05-02 19:41:02',
content: 'This is my Second post!',
user_Name: 'Abdelrahman',
user_id: '8721da2c-0028-430f-a995-0d03c8abb393'
},
{
id: 3,
title: 'test Title',
date_posted: '2021-01-06 08:48:01',
content: 'test Content',
user_Name: 'Abdelrahman',
user_id: '8721da2c-0028-430f-a995-0d03c8abb393'
},
{
id: 4,
title: 'test Title2',
date_posted: '2021-01-06 08:49:42',
content: 'test Content2',
user_Name: 'Abdelrahman',
user_id: '8721da2c-0028-430f-a995-0d03c8abb393'
},
{
id: 5,
title: 'test 3',
date_posted: '2021-01-06 08:54:14',
content: 'COntent 3',
user_Name: 'Abdelrahman',
user_id: '8721da2c-0028-430f-a995-0d03c8abb393'
}
but the output i see is not sorted at all
Usually you store the token as state in vuex. In your method you commit a mutation to set the token state, something like:
this.$store.commit('SET_TOKEN', res.data)
In the store you will have
state: {
token: '',
},
mutations: {
SET_TOKEN(state, payload) {
state.token = payload.token
}
This is just an example to be adjusted depending on response you get.
If you need to persist the state you should use also vuex persisted state
i am trying to update or remove a subdocument from a parent document using mongoose.
My code:
editSubscription(req, res) {
const token = req.headers.authorization;
jwt.verify(token, req.app.get('yourSecretKey'), function (err, payload) {
userModel.update({ _id: payload.user._id, "subscriptions._id": req.params.id }, { "$set": { "subscriptions.$": req.body } }, function (err, obj) {
console.log(obj)
})
})
}
The output of the console.log is
{ n: 0, nModified: 0, ok: 1 }
How should i do this? i know if its modified the nModified returns a 1. I can't find any docs on how to approach this or solve it and all the solutions here on stackoverflow i already tried, nothing is working.
A sample of a document in my collection:
id: '5db990daa05aa90de0c8b86b',
user:
{ role: 'User',
subscriptions: [
{ active: true,
_id: '5dbad05aaf232e2bdc033339',
name: 'Basic Fit',
price: 20,
paymentDate: '07-11-2020',
created: '2019-10-31T12:15:22.360Z',
updated: '2019-10-31T12:15:22.360Z' },
{ active: true,
_id: '5dbad2568bf56255a0f39bc7',
name: 'Netflix',
price: 10,
paymentDate: '07-11-2019',
created: '2019-10-31T12:23:50.141Z',
updated: '2019-10-31T12:23:50.141Z' } ]
],
_id: '5db990daa05aa90de0c8b86b',
fullname: 'Test naam',
email: 'test1#mail.com',
password:
'$2a$10$VzBnIcVraIRdmzy6rPHOX.7gGOXToTBNISLEfi429OfpRx02FxCaO',
birthDate: '02-12-1988',
created: '2019-10-30T13:32:10.276Z',
updated: '2019-10-30T13:32:10.276Z',
__v: 0 },
payload.user._id == the verified logged in user ID
req.params.id is supposed to be the subscriptionId im trying to edit
Try this:
editSubscription(req, res) {
const token = req.headers.authorization;
jwt.verify(token, req.app.get('yourSecretKey'), function (err, payload) {
userModel.update({ _id: ObjectId(payload.user._id), "subscriptions._id": ObjectId(req.params.id) }, { "$set": { "subscriptions.$": req.body } }, function (err, obj) {
console.log(obj)
})
})
}
I tried and im getting a 500 error response back..
When i remove the 'req.params.id' and i do it like this:
userModel.updateOne({ _id: payload.user._id }, { "$set": { "subscriptions.0.price":
req.body.price} }).then(user => {
console.log(user)
}).catch(err => {
console.log(err)
})
Then it works but since i have multiple items in an array i do not want to update only the first one..
I tried using a filter in the $set operator but im getting errors all over the place..
I am trying to make a populate query with mongodb (using mongoose as orm), and it doesn´t work 100%. I mean that I get all the entries from the document where I apply the populate... ALL, the entries that (on the crossed document) match with the match query (and I obtain a nested object), but I get the others too (and the nested object is null).
This is what I have done:
MODELS
var userSchema = Schema({
name: String,
email: String,
idiom: String
});
var ticketsSchema = Shema({
user_id:{ type:Schema.Types.ObjectId, ref:'User', required: true},
date: Date,
points: Number,
kart: String
});
POPULATE
tickets.find()
.populate(
{
path: 'user_id',
match:{name:"user1"}
}
).exec(function (err, result) {
if (err) return handleError(err);
res.send(result);
});
And a possible result could be:
Object[0]
__v: 0
_id: "5655b68ccbe953bc2a78da54"
date: "2015-11-25T13:24:28.561Z"
date: "2015-11-25T13:24:28.561Z"
points: 50
kart: "Senior"
user_id: Object
__v: 0
_company: 0
_id: "5655b656cbe953bc2a78da53"
name: "user1"
email: "user1#mail.com"
idiom: "es"
Object[1]
__v: 0
_id: "5655b732e0685c441fddb99b"
date: "2015-11-25T13:27:14.608Z"
points: 75
kart: "Pro Senior"
user_id: Object
__v: 0
_company: 0
_id: "5655b656cbe953bc2a78da53"
name: "user1"
email: "user1#mail.com"
idiom: "es"
Object[2]
__v: 0
_id: "56564613701da2981aa017d6"
date: "2015-11-25T23:36:51.774Z"
points: 75
kart: "Pro Senior"
user_id: null
Object[3]
__v: 0
_id: "565646ee701da2981aa017d8"
date: "2015-11-25T23:40:30.308Z"
points: 75
kart: "Pro Senior"
user_id: null
This isn´t exactly what I want to do... I would want that the two last doesn´t show up.
EDIT
I think I didn´t explain clear myself clearly... what I want to do is a JOIN query...
I have seen that I need use a mapreduce, I tried to do but I don´t found a place where it is explained for... dummies. All what I could do until now is this:
ticketsDB.find()
.populate(
{
path: 'user_id',
match:req.body
}
).exec(function (err, result) {
if (err) return res.send(err);
reslt=[];
for(var i in result)
if(result[i].user_id)reslt.push(result[i]);
console.log(reslt);
res.send(reslt);
});
Thank you.
Thanks #JohnnyHK by his comment, I found the solution here: Mongoose nested query on Model by field of its referenced model
And now the code is something like:
usersDB.find(req.body, function(err, docs) {
var ids = docs.map(function(doc) { return doc._id; });
ticketsDB.find({user_id: {$in: ids}})
.populate('user_id').exec(function (err, result) {
if (err) return res.send(err);
reslt=[];
for(var i in result)
if(result[i].user_id)reslt.push(result[i]);
res.send(reslt);
});
});
Where req.body is {name:"user 1"} or {email:"user1#user.com"}.
EDIT
I was thinking a little (sorry, it will happen no more ;p ), and I think that there is a way to remove the mapping of this query. Now my answer is this:
usersDB.find(req.body).distinct("_id", function(err, docs) {
ticketsDB.find({user_id: {$in: docs}})
.populate('user_id').exec(function (err, result) {
if (err) return res.send(err);
reslt=[];
for(var i in result)
if(result[i].user_id)reslt.push(result[i]);
res.send(reslt);
});
});
Could be that valid??
I have a model like this:
User:
{
name: "test",
phone: "1234567890",
password: "test",
email: "test#tester.com",
tasks: {
{ task: "do this", timestamp: "2015-08-01" },
{ task: "then this", timestamp: "2015-08-05" },
{ task: "finally this", timestamp: "2015-08-07" }
}
},
... (more users) ...
How can I get a specific user's details like name, email, and only 1 task the latest one?
User.find({phone: '1234567890', password: 'test'}, '_id name email tasks', function(err, user) {
if (err)
res.json({result: false});
res.json({result: !(user == null), object: user});
});
This returns all the tasks.
You could use $slice (projection)
User.find({
phone: '1234567890',
password: 'test'
}, {
name: 1,
email: 1,
tasks: {$slice: -1}
}, function (err, user) {
if (err) {
res.json({result: false});
return;
}
return res.json({result: !(user == null), object: user});
});
Note that tasks should be Array but not Object.
if you are inserting new task by push method of array. then you can get last record by getting length of tasks array and access last record by length - 1.
or you can refer this :
return document with latest subdocument only in mongodb aggregate
I like to update an entry in mongodb. But lodash only update one value in the array. I send this object to my node.js server:
{ _id: 5593df7c087e59a00c04cda3,
name: 'blueberry',
uuid: 'b9407f30-f5f8-466e-aff9-25556b57fe6d',
major: '12345',
minor: '12345',
position: 'Kantine',
__v: 18,
messages:
[ { _id: 5593df7c087e59a00c04cda4,
timeRange: [Object],
url: '',
message: 'j',
title: 'jv',
messageType: 'text' },
{ _id: 5593df7c087e59a00c04cda4,
timeRange: [Object],
url: '',
message: 'j',
title: 'jv',
messageType: 'text' } ] }
Here is the code for the update of the mongodb-entry:
// Updates an existing ibeacons in the DB.
exports.update = function(req, res) {
Ibeacons.findById(req.params.id, function (err, ibeacons) {
if (err) { return handleError(res, err); }
if(!ibeacons) { return res.send(404); }
var updated = _.merge(ibeacons, req.body);
updated.save(function (err) {
if (err) { return handleError(res, err); }
return res.json(200, ibeacons);
});
});
};
But I get this as result:
{ _id: 5593df7c087e59a00c04cda3,
name: 'blueberry',
uuid: 'b9407f30-f5f8-466e-aff9-25556b57fe6d',
major: '12345',
minor: '12345',
position: 'Kantine',
__v: 18,
messages:
[ { _id: 5593df7c087e59a00c04cda4,
timeRange: [Object],
url: '',
message: 'j',
title: 'jv',
messageType: 'text' },
{ _id: 5593df7c087e59a00c04cda4,
timeRange: [Object],
url: '',
message: 'j',
title: 'jv',
messageType: 'text' } ] }
Maybe someone can help me.
Ok I get it. the version of lodash was on 2.4.1 now I updated it to 3.1.0 and it works. :D