I'm developing a simple app with Node/Hapi/Mongodb, but running into a strange issue. Below is the route that handles adding/updating scores; when I send some data to this endpoint through Insomnia/Postman it works as expected. However, when this POST is coming from a different app I'm getting strange results; the value is always null for every field (again this only happens when the POST is coming from another site, but I've logged out the request payload and can see that the data is correct, just gets set to null when assigning to an object, or trying to use it a query)
server.route({
method: 'POST',
path: '/update-score',
handler: (request, h) => {
var scores = db.collection('scores');
var updateScore = new Promise((resp, rej) => {
console.log('payload ', request.payload);
scores.findOneAndUpdate({customerID: request.payload.customerID}, {$set: {customerID: request.payload.customerID, customerName: request.payload.customerName, highScore: request.payload.highScore}}, {upsert: true}, (err, res) => {
if (err) {
return rej(err);
}
else {
return resp(res);
}
})
});
return updateScore;
}
});
The console logs out the request payload correctly, but its null/undefined when the query tries to use it. I have also tried creating two objects, outside the mongo method call (like below), and after console logging these pre-defined objects out the value was null there as well; even though I can console.log the request.payload after defining these objects and the data is good.
server.route({
method: 'POST',
path: '/update-score',
handler: (request, h) => {
var scores = db.collection('scores');
var queryObj = {
customerID: request.payload.customerID
};
var updateObj = {
$set: {
customerName: request.payload.customerName,
highScore: request.payload.highScore
}
}
var updateScore = new Promise((resp, rej) => {
console.log('again ', request.payload);
scores.findOneAndUpdate(queryObj, updateObj, {upsert: true}, (err, res) => {
if (err) {
return rej(err);
}
else {
return resp(res);
}
})
});
return updateScore;
}
});
Logging the queryObj and valueObj would show the values are all null, even though I can log the request.payload and see the data correctly. Why can't I use the request.payload values anywhere?
Long story short, Insomnia/Postman sends an object as the POST body, but I was JSON encoding the POST from the app; just needed to parse that on the server!
Related
I have an Ldap Server running on Docker + ldapjs. This server is adding a set of records that I am trying to search for with the client.
A sample user object looks like below:
{
user: 'cn=first.last,ou=user_group,o=main',
info: {
cn: 'first.last',
email: 'first.last#mail.com'
}
}
The options would look like this:
let opts = {
scope: 'base',
attributes: ['dn', 'sn', 'cn', 'user', 'info']
};
I'm using this code in a class, so I bind in the constructor, after initializing the client:
constructor(url) {
client = ldap.createClient({
url: url
});
client.on('error', (err) => {
log.error(`${err}`);
});
client.bind(username, password, function (err) {
if (err) {
log.error(`${err}`);
}
});
log.info('Client Initialized.');
};
And my search code:
return new Promise((resolve, reject) => {
var record = {};
client.search(username, opts, function (err, res) {
res.on('searchEntry', function (entry) {
log.info(`Record Retrieved: ${JSON.stringify(entry.object)}`);
record = entry.object;
});
res.on('error', function (err) {
log.error(`Error: ${err.message}`);
});
res.on('end', function (result) {
if (err) {
reject(err);
}
else {
log.info(`Status: ${result.status}`);
resolve(record);
}
});
});
});
The issue I'm experiencing is that the code will always resolve on end when I make a search request from the client, which means that I never get a match, although it's definitely there.
I've tried:
Binding inside and outside the promise instead. No difference.
Changing the user structure and username used in client.search. No difference.
Searching for only 'cn=first'. I do get an error that it doesn't exist, which is good.
Adding a filter in options and changing the parameters there, but still no result.
I connect to the server ok, bind is ok as well, so I think I'm either doing the search wrong, or the way I have structured the users in the server is not proper.
Added screenshot showing server logs: The user added in the entry looks like it has a different name, but I changed it to match in the data.
I've found the issue, which was related to the structure I was using in my records, I've solved it using an ldapts client instead, but the same logic can be used in an ldapjs client:
Specifically:
This is a record in my ldapjs Server:
{
name: 'John Doe',
uid: 'john.doe',
dn: 'uid=john.doe, ou=users, o=server',
email: 'john.doe#email.com',
userprincipalname: 'cgi-doej',
}
This is how I search for it:
let attributes = ['cn'], filter = `(email=${email})`
const { searchEntries, searchReferences } = await this.client.search(searchDN, {
scope: 'base',
filter: filter,
attributes: attributes
});
This has solved my issues.
so i wrote this function to check weather the login given exists already in the odoo database, but it always returns undefined, my guess is that it returns undefined because the return line is inside the calling method,
i tried to use it as an async function but it didn't work either, i need to know how can i make the return line refrences to the global function scope and not just the calling method.(i need the calling method to get the full list of users from the odoo database). Any suggestions ??
`
function user_exist(email){
odoo.connect(function (err) {
if (err) { return console.log(err); }
console.log('Connected to Odoo server.');
var inParams = [];
inParams.push([['active', '=', true]]);
var params = [];
params.push(inParams);
// 4- Read
odoo.execute_kw('res.users', 'search', params, function (err, value) {
if (err) { return console.log(err); }
var inParams = [];
inParams.push(value); //ids
inParams.push(['login']);
var params = [];
params.push(inParams);
odoo.execute_kw('res.users', 'read', params, function (err2, value) {
if (err2) { return console.log(err2); }
for (let i = 0; i < value.length; i++) {
if (email == value[i].login){
return "User exist"
}
}
return "user doesn't exist"
});
});
});
}
`
if you are making your api you need to learn how to use postman, and in this solution we are using axios:
var axios = require('axios');
let data = JSON.stringify({
"jsonrpc": "2.0",
"method":"call",
"params": {
"service":"object",
"method":"execute",
//arg1 : your database name must be "string"
//arg2 : id of the user admin must be "int" ex:1 or 3 or 66
//arg3 : password of the user admin must be "string"
// admin:admin do like this 2:"admin"
//arg4 : object or model name ex:"res.users"
//arg5 : orm methods ex:"search,search_read..."
//"args":["arg1",'arg2',"arg3","arg4","arg5"]}
"args":["app",2,"admin","res.users","search_read",[],[]]
}
});
let config = {
method: 'get',
url: 'http://localhost:8069/jsonrpc',
headers: {'Content-Type': 'application/json'},
data : data
};
axios(config).then(response => {handleResult(response)})
function handleResult(data) {
// if you want you can remove result
console.log(JSON.stringify(data.data.result));
}
i hope this will be help you
I have a NODE.JS api using expressjs that connects to an SQL Server, and I want to use it in an angular project. I make use two files, a route file and a controllers file. My route file is as follows:
module.exports = (app) => {
const UsrContrllr = require('../Controllers/users.controllers');
//1. GET ALL USERS
app.get('/api/users', UsrContrllr.func1);
//2. POST NEW USER
app.post('/api/user/new', UsrContrllr.func2);
};
And my controllers file is given below:
const mssql = require('mssql');
exports.func1 = (req, res) =>
{
// Validate request
console.log(`Fetching RESPONSE`);
// create Request object
var request = new mssql.Request();
// query to the database and get the records
const queryStr = `SELECT * FROM USERS`;
request.query(queryStr, function (err, recordset) {
if (err) console.log(err)
else {
if (recordset.recordset.toString() === '') {
res.send('Oops!!! Required data not found...');
}
else {
// send records as a response
res.send(recordset);
}
};
});
};
exports.func2 = (req, res) =>
{
// Validate request
console.log(`INSERTING RECORD ${req}`);
// create Request object
var request = new mssql.Request();
// query to the database and get the records
const queryStr = `INSERT INTO GDUSERS (USERCODE, PASSWORD, LANGUAGE, USERCLASS, FIRSTNAME, LASTNAME, CONTACTNO) VALUES ('${req.body.usercode}', '${req.body.password}', 'EN', '0', '${req.body.firstname}', '${req.body.lastname}', '${req.body.contactno}');`;
request.query(queryStr, function (err, recordset) {
if (err) console.log(err)
else {
if (recordset.recordset.toString() == '') {
res.send('Oops!!! Required data not found...');
}
else {
// Send records as response
res.send(recordset);
}
};
});
};
The GET request works well, but when I try to run the POST request directly from the angular application, I get an error stating
Cannot GET URL/api/user/new
The angular code in my angular project is:
signup() {
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
console.log(this.user); //User details come from a form
this.http.post(“URL", this.user, options)
.subscribe(
(err) => {
if(err) console.log(err);
console.log("Success");
});
}
I’m not sure whether the angular code I’m using, is right or not, and I don’t know where I’m going wrong. How does one exactly send a http POST request from an Angular project?
this i the way i handled my user signup with http.post calls. my approach is slightly different when signing up user because i am using a promise instead of observable (which i normally use for my servicecalls). but i will show you both ways.
createUser(user: User): Promise < string > {
const promise = new Promise < string > ((resolve, reject) => {
const userForPost = this.createUserForPost(user);
this.http.post(environment.backendUrl + '/api/user/signup', userForPost, this.config).toPromise < HttpConfig > ()
.then(createdUser => {
}).catch(error => {
console.log(error);
});
});
return promise;
}
here another example with an observable
createForumPost(forumPost: ForumPost) {
this.http.post < { message: string, forumPostId: string } > (environment.backendUrl + '/api/forumPosts', forumPost).subscribe((responseData) => {
const id = responseData.forumPostId;
forumPost.id = id;
});
}
i defined my URL somewhere else and then just use the environment.backedUrl + 'path' to define my path (the same as the path in your backend controller)
this is one of my first answers here on SO. i am sry if it is a bit messy
i hope i was able to help with my examples :)
community. I am trying to implement a live search using the autocomplete library but every try is unsuccessful. I get every time a 500 server error. Every assistant is appreciated because I am new in coding.
I have a simple model for an article with title and body and I would like to show suggestions when the user search for an article
model/article.js
// Method to construct the json result set
module.exports.buildResultSet=function(docs) {
var result = [];
for(var object in docs){
result.push(docs[object]);
}
return result;
}
routes/article.js
router.get('/search', function(req, res){
encyclopediaModel.getMyArticlesByName(theRequester, function (pError, pFoundedArticles) {
if (!pError) {
// Method to construct the json result set
var result = encyclopediaModel.buildResultSet(pFoundedArticles);
res.json(result);
} else {
return res.json(JSON.stringify(pError), {
'Content-Type': 'application/json'
}, 404);
}
},req.query.title)
});
//Ajax call
$("#search-query").autocomplete({
source: function (request, response) {
$.ajax({
url: "/encyclopedia/search",
type: "GET",
data: request, // request is the value of search input
success: function (data) {
response( data );
console.log('success', data);
}
});
},
// The minimum number of characters a user must type before a search is performed.
minLength: 3,
// set an onFocus event to show the result on input field when result is focused
focus: function (event, ui) {
this.value = ui.item.label;
// Prevent other event from not being execute
event.preventDefault();
},
select: function (event, ui) {
}
});
<input id="search-query" type="text" placeholder="Articles...">
module.exports.getMyArticlesByName = function (requester, callback, pTitle){
var regex = new RegExp(pTitle["term"], 'i');
article.find({title: regex}, { 'title': 1 }).sort({"updated_at":-1}).sort({"created_at":-1}).limit(20).exec(callback);
}
I have the following method in my AccountsApiHandler module, and I would like to write a test that whether the accounts are correctly returned. If I am calling this method from one of my tests, how can I retrieve the list of accounts?
AccountsApiHandler.prototype.accounts = function (req, res) {
var self = this
/*
* Get list of accounts
*/
if (req.method === 'GET') {
return self.server.accounts.listAccountAsJson()
.pipe(JSONStream.stringify())
.pipe(res);
}
};
Here is my test, using the hammock npm module to generate my mock request and response objects. This test assumes that the existing accounts are already in the test's database:
var test = require('tape')
var hammock = require('hammock')
var accountsApiHandler = require('../../handlers/accounts-api')()
test('create initial accounts', function (t) {
request = hammock.Request({
method: 'GET',
headers: {
'content-type': 'application/json'
},
url: '/somewhere'
})
request.method = 'GET'
request.end()
response = hammock.Response()
var accounts = accountsApiHandler.accounts(request, response)
console.log("test.accountsApiHandler: accountsApiHandler.accounts: accounts", accounts) // `accounts` is undefined!
var accountsList = []
var actualAccounts = getAccountsFromDatabase() // the actual accounts from our db
accounts
.on('data', function (data) {
accountsList.push(data)
})
.on('error', function (err) {
t.fail(err)
})
.on('end', function () {
console.log("results:", accountsList)
t.equals(accountsList, actualAccounts)
})
}
When I run this test, my accounts stream is empty, thus giving me an empty list of accounts for accountsList. Is there a decent way to get the accounts data from my AccountsApiHandler.accounts method?
It looks like my implementation in the above question was close, but instead of streaming the accounts like this:
accounts
.on('data', function (data) {
accountsList.push(data)
})
.on('error', function (err) {
t.fail(err)
})
.on('end', function () {
console.log("results:", accountsList)
t.equals(accountsList, actualAccounts)
})
}
I needed to stream the response itself, which also doesn't have a data event:
accounts
.on('end', function (err, data) {
t.equals(data, actualAccounts)
})
Hopefully this helps someone. More documentation is in the hammock site on npm