i use chef-api for express js and i wanna get only the ip address of node " server 1 " from a chef-server
im sending a request like this
Code :
chef.partialSearch("node", "name:server1",{"ip":"ipaddress"} ,function(err, res){
if (err){console.log(error);}
else{console.log(res);}
});
Or
chef.partialSearch("node", { q: "name:server1"} ,{"ip":"ipaddress"} ,function(err, res){
....
});
=> Response :
received status code 400 invalid value 'ipaddress' for no_key
function in code source :
partialSearch: function(index, qs, data, fn){
http_methods.post([config.host_url, "search", index].join("/"), qs, data, function(err, response){
return fn(err, response);
});
}
and i cant understand the correct syntax for the request (http) from the official website doc api_chef_server
Can you please give a valid syntax with example .
Thanks
What you probably want is something like this:
chef.getNode('server1', function(err, node) {
if(err) throw err;
console.log(node.automatic.ipaddress);
});
Finaly i found the correct syntax for both request
Simple Search :
chef.search("node", {q: "name:server1" }, function(err, res){
if (err){console.log(error);}
else{console.log(res);}
});
Partial Search :
chef.partialSearch("node", "chef_environment:prod", { name: ['name'] , 'ipaddress': ['ipaddress'] }, function(err, res){
if (err){console.log(error);}
else{console.log(res);}
});
Hope this can help someone else who is still looking .
Related
I have this route :
router.get('/api/sellOffer/seller/:sellerId', sellOfferController.getAllSellOffersBySellerId);
Pointing there :
exports.getAllSellOffersBySellerId = (req, res, next) => {
SellOffer.find({
sellerId : req.params.id,
}).then(
(sellOffers) => {
res.status(200).json(sellOffers);
}
).catch(
(error) => {
res.status(400).json({
error: error
});
}
);
};
And retriving me this (status 200 OK) :
null
However I have this in my DB :
_id:615ea0157d3bb20016da036c
price:800
sellOfferAccept:true
productId:"615ea0147d3bb20016da036a"
sellerId:"615b1e1bfde0190ad80c3477"
couponDownloaded:false
createDate:2021-10-07T07:21:57.234+00:00
__v:0
I've tried many way of writing the request :
https://hiddenlink/api/sellOffer/seller/?id=615b1e1bfde0190ad80c3477
https://hiddenlink/api/sellOffer/seller/?sellerId=615b1e1bfde0190ad80c3477
None work. Any idea please ?
Thanks a lot for any anwser and have a good day ! :)
Assuming you're using express,
/api/sellOffer/seller/:sellerId resolves to /api/sellOffer/seller/615b1e1bfde0190ad80c3477 - it doesn't use query parameters.
Also, consider whitelisting values when returning the JSON response, you don't want some accidental extra data being returned.
I am trying to send/update data to mongoDB database via AAJAX call but the command is not reaching theere. I have tried debugging using alert in between the code but the command is not reaching there. Means AJAX call doesn't get executed.
Below is my AJAX POST request code:
var text = "Done";
var data = {
selectedValue: text
}
$ajax({
method: 'POST',
url: '/update-sources',
dataType: 'text/json',
data: data,
success: function(data){
console.log(data);
alert("Working!!")
}
});
And Below is the /update-sources route code:
router.post('/update-sources', function(req, res, next) {
console.log("/Update-Sources")
User.findOneAndUpdate({email: req.user.email}, {$set:{status:data.selectedValue}}, {new: true}, (err, doc) => {
if (err) {
console.log("Something wrong when updating data!");
}
else
{
res.render('taskswriter');
console.log(doc);
return "Great Working!";
}
});
});
What mistake I am doing?
Would be great if you shared browser's console output, but trying to execute attached client-side snippet, I got the following error:
VM219:7 Uncaught ReferenceError: $ajax is not defined
at <anonymous>:7:1
You've got a typo there - it should be $.ajax as you are accessing a function from within jQuery namespace (https://api.jquery.com/jQuery.ajax/)
I am new to node.js. Following is my GET request:
router.get('/EPODS/Product/:id?', function(req, res) {
if(req.params.id){
var inputIdObj={'ProductEBM.DataArea.Product.ProductGroupID': req.params.id};
} else {
res.status(500).json({
"Error": "No input given Try give something or Check for the case sensitive in the input query fields"
});
res.end();
return;
}
db.collection('Product').findOne(inputIdObj,function(err, doc) {
if (err) {
console.log("No record found for the given input");
res.end("No record found for the input data- Please check again");
} else {
res.send(doc);
}
});
});
I am receiving null value when I hit the request in POSTMAN but the query works in MONGODB.
Please Help!
Thanks in Advance.
When you get the parameter of the GET request, you get the ID in a String format, which will not work if you use ObjectID. First, cast your param in ObjectID (you can import the object with require('mongodb').ObjectID) and so create: new ObjectID(req.params.id)
I've been trying to get the uber price estimates endpoint working, but I'm stuck on an error that leads me to a blank page saying, "Bad Request." The console also says "callback not a function" but I can't seem to find out what is wrong.
My route:
// Get an upfront fare before requesting a ride
app.get('/v1.2/estimates/price', function(request, response) {
// extract the query from the request URL
var query = request.query;
// if no query params sent, respond with Bad Request
if (!query || !query.lat || !query.lng) {
response.sendStatus(400);
} else {
uber.estimates.getPriceForRouteAsync( {
"product_id": "33de8094-3dc4-4ca9-8f67-243275f57623",
"start_latitude": "38.9597897",
"start_longitude": "-94.60699369999999",
"end_latitude": "39.010969",
"end_longitude": "-94.61509899999999"
})
.then(function(res) {
log(res);
})
.error(function(err) {
console.error(err);
});
}
});
Any help is appreciated.
Please check out the README for node-uber. The method does not take a JSON object but the arguments in the method call:
uber.estimates.getPriceForRouteAsync(38.9597897, -94.606994, 39.010969, -94.615098)
.then(function(res) { console.log(res); })
.error(function(err) { console.error(err); });
Also, the product ID is not needed as the /estimates/price endpoint returns an array of estimates for each product.
I'm simply trying to use sails.socket.emit() to send a message and I get this:
error: Sending 500 ("Server Error") response:
Error (SAILS:HOOK:SOCKETS:USAGE):: `sails.sockets.get()` cannot lookup socket w/o an id (got: `undefined`)
The code in the controller:
sayHiToFriend: function(req, res) {
var friendId = req.param('id');
sails.sockets.emit(friendId, 'privateMessage', {from: req.session.userId, msg: 'Hi!'});
res.json({
message: 'Message sent!'
});
}
And client side:
io.socket.get('/user/sayHiToFriend', function serverSays(err,users){
console.log('with headers: ', JWR.headers);
console.log('and with status code: ', JWR.statusCode);*/
if (err)
console.log(err)
console.log(JSON.stringify(users));
});
Do I have to pass the id as data to io.socket.get ? If so how?
edit: solution:
sails.sockets.emit(sails.sockets.id(req.socket), 'welcome', {custom: 'data here'});
I had the same problem. It seems like Sails.js has some internal problems or something else we cannot control...in my case few hours ago all worked fine and now it gives that error. I solved it simply compacting the array of the IDs with lodash:
User.findOne({'id': req.body.id}, function(error, user) {
var subscribers = _.compact(User.subscribers(user));
sails.sockets.emit(subscribers, "notification", {msg: req.body.msg});
});
and now everything works fine! Hope it helps!