I am inserting login tokens into my database, but I keep getting 'Error: key services.resume.appLoginToken.date must not contain '.'
My code below is :
if(!result.services.resume || result.services.resume.appLoginToken == null){
var tokenStr = email + Math.random(1,100);
tokenStr = Base64.encode(tokenStr);
queryStr = {
"services.resume.appLoginToken.date": Date(),
"services.resume.appLoginToken.base64Token": tokenStr
};
collectionUser.insert(queryStr, function(err, result) {
if(err)
{
console.log('Error:'+ err);
return;
}
});
}
Any help would be great, thank you
Related
I copied documents from a local database to my production database and when I try to get the document by Id by running model.findOne({_id : id}) and mongoose returns nothing. I am copying the documents over with the same Id, but I also tried with a new Id. I can find the document in the database and confirm that the JSON is correct, the Id is correct, etc and it won't find it. The documents I did not copy and where generated via my app still query fine with the findOne command. So, I have no idea what's going on
any help is greatly appreciated, thanks
groups.crud
getGroupById(id: string) {
logger.debug(".getGroupById id: " + id);
return new Promise(function(resolve, reject) {
GroupsModel.findById(id)
.populate('createdBy')
.then(function (group) {
logger.debug(".getGroupById");
if(group.createdBy.privacySettings.useUserName) {
group.createdBy.firstName = '';
group.createdBy.lastName = '';
}
resolve(group);
})
.catch(function(error) {
reject(error);
});
});
}
groups.routes
getGroupById(req, res, next) {
logger.debug('.getGroupById: BEG');
let id = req.params.id;
return groupsCrud.getGroupById(id)
.then(function(group) {
if(group) {
logger.debug('.getGroupById: get by id success');
let response = {
data : group
}
logger.debug('.getGroupById: response: ' + response);
res.json(response);
}
else {
logger.debug('.getGroupById: get by id failed 1');
res.status(404).json({ status : 404, message : "Group not found."});
}
})
.catch(function(error) {
logger.debug('.getGroupById: get by id failed 2 err = ' + JSON.stringify(error, null, 2));
res.sendStatus(404);
});
}
I have written a authentication service to authenticate user name and password in a Login Page. The code below is the service.
public login(credentials) {
if (credentials.username === null || credentials.password === null) {
return Observable.throw("Please insert credentials");
} else {
let apiURL = 'http://localhost/timeclock/api/login?usercode=' + credentials.username +
'&password=' + credentials.password ;
return Observable.create(observer => {
this.http.get(apiURL).map(res => res.json()).subscribe(data => {
if (data.success === 'true')
{
this.currentUser.name = data.data.user_name;
this.currentUser.email = data.data.user_email;
observer.next(true);
observer.complete();
} else {
observer.next(false);
observer.complete();
}
});
});
}
}
When the user name and password is submitted, the URL is correctly called with the right parameters.
The http call takes very long time to complete. Also, no response is returned.
It takes only two or three seconds to get the response when I call the URL with the same parameters in the browser.
Any idea on how to fix this?
You don't need to create a new Observable you can refactor like this.
public login(credentials) : Observable<boolean> {
if (credentials.username === null || credentials.password === null) {
return Observable.throw("Please insert credentials");
} else {
let apiURL = 'http://localhost/timeclock/api/login?usercode=' + credentials.username +
'&password=' + credentials.password ;
return this.http.get(apiURL).map(res => res.json())
.map(data =>
{
if(data.success){
this.currentUser.name = data.data.user_name;
this.currentUser.email = data.data.user_email;
return true;
}else{
return false;
}
});
}
}
I have a Parse class Comment. Each Comment is submitted by a ParseUser and I wanna track in my parse user how many comments he has submitted, so I've created the following triggers in Parse Cloud Code:
/*
* ------------- AFTER SAVE
*/
Parse.Cloud.afterSave("Comment", function(request) {
//if it's the first save
if(!request.object.existed()) {
//increment the User's comments
query = new Parse.Query(Parse.User);
query.get(request.object.get("createdBy").id, {
success: function(user) {
user.increment("comments");
user.save();
}, error: function(error) {
console.log("Error searching the User");
}
});
}
});
/*
* ------------- AFTER DELETE
*/
Parse.Cloud.afterDelete("Comment", function(request) {
//decrement the User's comments
console.log("The user who submitted the comment has the id: " + request.object.get("createdBy").id);
query = new Parse.Query(Parse.User);
query.get(request.object.get("createdBy").id, {
success: function(user) {
console.log("Retrieved the user. He has username: " + user.get("username"));
user.increment("comments", -1);
user.save();
}, error: function(error) {
console.log("Error searching the user - error afterDelete#Comment 2");
}
});
});
The problem is that the afterSave trigger works, but the afterDelete doesn't and I can't figure out why. I can retrieve the User because the two console.log show out which is the correct user, but I can't save it, after the increment(user, -1). Thanks in advance.
EDIT:
Here the User trigger:
/*
* ------------- BEFORE SAVE
*/
Parse.Cloud.beforeSave("User", function(request, response) {
var comments = request.object.get("comments");
if(comments == null || comments < 0)
request.object.set("comments", 0);
var itemSaved = request.object.get("itemSaved");
if(itemSaved == null || itemSaved < 0)
request.object.set("itemSaved", 0);
var username = request.object.get("username");
if(username.length < 6 || username.length > 20)
response.error("username must be longer than 6");
else {
response.success();
}
});
/*
* ------------- AFTER DELETE
*/
Parse.Cloud.afterDelete("User", function(request) {
query = new Parse.Query("Comment");
query.equalTo("createdBy", request.object.id);
query.find({
success: function(comments) {
Parse.Object.destroyAll(comments, {
success: function() {},
error: function(error) {
console.error("Error deleting related comments " + error.code + ": " + error.message);
}
});
},
error: function(error) {
console.error("Error finding related comments " + error.code + ": " + error.message);
}
});
});
Per Parse's documentation, "If you want to use afterDelete for a predefined class in the Parse JavaScript SDK (e.g. Parse.User), you should not pass a String for the first argument. Instead, you should pass the class itself."
//router
app.get('/retrieve_report', function(req, res) {
var retrieved = retrieve_report(req, res);
res.render('retrieve_report.ejs', {
'report' : retrieved
});
});
//Load up the report model
var Report = require('../models/report');
console.log('Report ' + Report.schema);
//expose this function to our app using module.exports
//query
module.exports = function(req, res) {
//console.log('param ' + res.send);
var query = Report.findById(req.param('id'), function(err, doc) {
if(err) {
throw err;
}
else {
console.log('doc ' + JSON.stringify(doc));
res.send(doc);
}
});
}
//app.js
var retrieve_report = require('./config/retrieve_report');//which is the above code
I want to return the document to the router so that I can put its information into my view. I tried "res.json(doc), but that gave me the error, "throw new Error('Can\'t set headers after they are sent.');" Everyone says to use a callback function, but aren't I using a callback function here?
As your error says:
but that gave me the error, "throw new Error('Can\'t set headers after they are sent.');"
Means you are trying to send data the twice.
Sample code:
app.get('/retrieve_report', function(req, res) {
var query = Report.findById(req.param('id'), function(err, doc) {
if(err) {
throw err;
}
else {
console.log('doc ' + JSON.stringify(doc));
res.send(doc);
}
});
This should work..
function find()
{
var id = $('#Id').val();
$.getJSON("api/questionsanswer/" + id,
function (data)
{
var str = data.Username + ': $ ' + data.Questions;
$('#questionanswer').text(str);
})
.fail (
function (jqXHR, textStatus, err) {
$('#questionanswer').text('Error: ' + err);
});
}
I try to get the data it display "null: null"
so it seems that data.Username and data.Questions wasn't capture... what is wrong with my code??