To avoid making multiple http calls from an Angular app, if I want to make one call and send multiple id strings to a node js back-end for deletion, should I be sending them as JSON?
If anyone can point me to a good example that would be great.
thanks
You also try with pass the array of ids in the request.
Example :
var idArray = [1,2,3,4,5,6];
$http(
method: 'DELETE',
url: '/items',
params: {
id: JSON.stringify(idArray)
}
)
You can just send the ID's in a JSON Format as shown below
id:"1000,1001,1002"
and in NodeJS if you are using MYSQL the following query works for delete
let ids = req.body.id;//To get the id sent in JSON Format
delete from table_name where id in (ids)
Comment below if u need any further guidance
Related
I know how to connect to Infusionsoft with Python 3 and how to process the following simple example:
#Set up the contact data we want to add
contact = {}; #blank dictionary
contact[“FirstName”] = “John”;
contact[“LastName”] = “Doe”;
contact[“Email”] = "john#doe.com";
contact[“Company”] = “ACME”;
But how do I mass update the WHOLE database? e.g. If I want to update ALL The Phone1 fields with an extra bit of code using IF statements.
Using Infusionsoft API you can only update contacts data one by one, sending a separate request per contact. Exact request depends on which type of API you use: REST or XML-RPC
I'm using xero-node npm package & looks like it will save me a ton of time.
I want to create OR update multiple contacts but not sure how. I'm hoping someone from Xero monitors this tag.
var contacts = [];
var contact = { "Name": "ABC", ContactNumber:"code123"};
contacts.push(xeroClient.core.contacts.newContact(contact));
const retVal = await xeroClient.core.contacts.saveContacts(contacts);
If I run it once , it creates the contact ( or multiple if I add to the array ) . I want to update the contact using my code ( not the xero generated id - because then I would need to store that it my other system).
If I run it a second time , it fails. I assume that is because it is doing a PUT instead of a POST..?
Here are the docs.
https://github.com/XeroAPI/xero-node/blob/2a1ec34888e998cabd72aa79fa58a5b14f2c9cd5/docs/Contacts.md
You are correct.
Here are the docs on Contacts:
https://developer.xero.com/documentation/api/contacts
See this section:
PUT Contacts
Use this method to create one or more contact records. This method works very similar to POST Contacts but if an existing contact matches your ContactName or ContactNumber then you will receive an error.
ContactNumber is unique. So you're trying to create two contacts with the same ContactNumber.
I think saving the contact like in the example here would help: https://github.com/XeroAPI/xero-node/blob/2a1ec34888e998cabd72aa79fa58a5b14f2c9cd5/docs/Contacts.md
I am new to Cloudant but have found it useful for a first stage of IoT data. But I need to subscribe to changes based on an id field that is separate from the _id and is unique to the sensor that is sending the data. The examples that I’ve seen so far haven’t helped with this problem. What I’m doing now is sending a separate json doc for each post, so it should return new docs with this sensor id. The json docs sometimes come in by the second but it can be hours as well.
I’m using c# in a .Net web app. The code below creates a call to the Cloudant database and returns the data that I want based on an index that was created for the field SensorID,
json =
{{
"selector": {
"SensorID" : "h7365cf3-17bc-4422-b436-f7bcf12b2e2a"
},
"fields": [
"Data"
]
}}
url = My Cloudant url + ” /_find”.
This returns all docs with the sensorID field that corresponds to the SensorID value in the json query, but just the json object of each doc nested in the Data field.
using (WebClient client = new WebClient())
{
byte[] postBytes = System.Text.ASCIIEncoding.UTF8.GetBytes(json.ToString());
client.UseDefaultCredentials = true;
client.Credentials = new NetworkCredential(username, password);
client.Headers[HttpRequestHeader.ContentType] = "application/json";
var response = client.UploadData(url, "POST", postBytes);
JObject iJson = JObject.Parse(client.Encoding.GetString(response));
return parseIncoming(iJson);
}
When the call is to My Cloudant url + “GET /_DB_UPDATES”, it returns information regarding changes to the whole database. This can be set up as a continuous feed.
I was hoping that this meant that i could subscribe to changes in documents to get new data coming, like Redis Pub/Sub. I’m starting to think that this might not be the case, but if anybody can show me how to do it I would be grateful.
As #adasilva70 said, you need to use the _changes feed.
You can filter changes with an appropriate filter function (so that only changes regarding the documents you're interested in show up).
You can get all updates since a given sequence point (everything since the last data you got) and/or you can use long polling or continuous mode for instant notifications.
I have a Node.js application with stores posts written by authors in a Redis database. The post is given a unique ID (the variable messageId), and two values are given: the message and the author. The first part of the code looks like so:
redisClient.hmset(messageId, "message", req.body.message, "author", req.body.author);
So far, so good. What I want to do is be able to retrieve the value of a specific key. I know I can use hgetall() to get all the keys and values for messageId, and I know I can use hkeys to get all the keys for messageId. If I know there will be a key called "message", how can I retrieve the value from message A) from the Redis client, and B) through Node.js?
NOTE: I have seen this: Redis + Node.js - how do I retrieve the values. It does not tell me what I need.
There is hget to retrieve a single value, and hmget to retrieve multiple values.
See Redis commands for hash. The node.js client should support them all.
Something like:
redisClient.hget(messageId, "message");
redisClient.hmget(messageId, ["message", "author"]);
Main differences between req.query and req.param in Express
How are Both different from each other
When to use then in what cases
Suppose a client sends say Android (Key,value) pair in the request ........ which one to use ?
[EDIT]
Suppose android sends a POST request -> Intention is to send (Key,Value) to client and the server should perform a database query based on the value in the server and return JSON response
Look:: at this question for the program i referenced:: Simple Express program for querying a result
req.query will return a JS object after the query string is parsed.
/user?name=tom&age=55 - req.query would yield {name:"tom", age: "55"}
req.params will return parameters in the matched route.
If your route is /user/:id and you make a request to /user/5 - req.params would yield {id: "5"}
req.param is a function that peels parameters out of the request. All of this can be found here.
UPDATE
If the verb is a POST and you are using bodyParser, then you should be able to get the form body in you function with req.body. That will be the parsed JS version of the POSTed form.
req.query is the query string sent to the server, example /page?test=1, req.param is the parameters passed to the handler.
app.get('/user/:id', handler);, going to /user/blah, req.param.id would return blah;
I would suggest using following
req.param('<param_name>')
req.param("") works as following
Lookup is performed in the following order:
req.params
req.body
req.query
Direct access to req.body, req.params, and req.query should be favoured for clarity - unless you truly accept input from each object.
Ref:http://expressjs.com/4x/api.html#req.param
Passing params
GET request to "/cars/honda"
returns a list of Honda car models
Passing query
GET request to "/car/honda?color=blue"
returns a list of Honda car models,
but filtered so only models with an stock color of blue are returned.
It doesn't make sense to add those filters into the URL parameters (/car/honda/color/blue) because according to REST, that would imply that we want to get a bunch of information about the color "blue". Since what we really want is a filtered list of Honda models, we use query strings to filter down the results that get returned.
Notice that the query strings are really just { key: value } pairs in a slightly different format: ?key1=value1&key2=value2&key3=value3.