I had ran the codes shown below. The 1st one runs but 2nd one does not
Can anyone please tell me the reason behind it.
//This runned successfully
updatePost(req,res)
{
let postId = req.params.postId
let posts = req.store.posts
posts[postId] = req.body
res.status(200).send(posts[postId])
}
//This gave error
updatePost(req,res)
{
req.store.posts[req.params.postId]=req.body
res.send(200).send(req.store.posts[req.params.postId])
}
Without knowing the error message....your last line is res.send(200).send(req.store.posts[req.params.postId]),
When it gets to ".send(req.store.posts[req.params.postId])" the response has already been sent.
Try changing it to res.status(200).send(req.store.posts[req.params.postId])
Like you have in the first block of code.
If this isn't your problem (maybe thats just a typo in your question and not in your code), please share the error message and I'll update my answer.
Related
I am using web3js to subscribe to logs, I listening to swap events, the problem is that the .on(data) is so fast in giving data JavaScript can not keep up. lets say I add a variable let count = 0; each time I get a new log I increase the number ++count, sometimes the logs come so fast I get a double number.
The real problem is I need it to be in the exact order as it is coming in, that's why I give the number to each log, but that does not work.
How would I make sure that each data item I get from the log events that they are in order?
I tried to create a promise sequence
let sequence = Promise.resolve();
let count = 0;
web3.eth.subscribe('logs', {
fromBlock: block,
topics: [
[swapEvent]
]
}).on('data', (logData)=>{
sequence = sequence.then(()=>{
++count
processData(logData)
})
});
function processData(){
return new Promise(resolve=>{
// do some stuff
resolve();
})
};
In a simple test with a loop and random time to resolve this works fine, but in the actual code with socket it does not keep the order.
Anyone has some idea how I can make the socket data keep in order and process one by one?
Not sure why but my problem got solved with this.
sequence = sequence.then(()=>processData(logData))
before it was
sequence = sequence.then(()=>{
processData(logData)
})
Now its doing all in sequence.
We have implemented a REST API using Restify and Node.js. There is a current issue where we have a certain set of parameters and when tested in environment1 it works properly. However, when the same set of parameters are used and tested in environment2, the API returns an error that there are missing parameters. After restarting the API, it starts working again. Nothing appears in the logs though.
Sample body parameters:
{
"records" : [
{"param1" : "param1val", "param2" : "param2val"},
{"param2" : "param2val", "param2" : "param2val"}
]
}
This is how the values are being accessed.
let data = req.body
let records = data.records
if (records == undefined) {
res.send(...)
return
}
else {
// use the values of records
}
So what happens is sometimes, the code for returning an error message that there are no parameters are triggered (although in reality the parameters are complete) from time to time. But after a while, using the same set of parameters, it proceeds successfully.
Restify version is 7.7.0. Node.js version is 10.15.3.
Has anyone encountered this before? What needs to be done to resolve this?
I'm following the example from the Bot Framework pages, under 'Handle User Actions' (https://learn.microsoft.com/en-us/azure/bot-service/nodejs/bot-builder-nodejs-dialog-actions?view=azure-bot-service-3.0)
// Order dinner.
bot.dialog('orderDinner', [
function(session, args, next){
if(args && args.isReloaded){
// Reload action was triggered.
}
session.send("Lets order some dinner!");
builder.Prompts.choice(session, "Dinner menu:", dinnerMenu);
}
//...other waterfall steps...
])
// Once triggered, will restart the dialog.
.reloadAction('startOver', 'Ok, starting over.', {
matches: /^start over$/i,
dialogArgs: {
isReloaded: true;
}
});
and after reloading the dialog args.isReloadedis always undefined. That is, it doesn't seem that the framework is passing through what is put in dialogArgs. Any clues as to what I might be missing? I'm using v 3.15 (or rather, the people for whom I'm working are using 3.15) -- was this something that was introduced in a later version 3, that is, after 3.5? Or is something just going wrong?
Any help much appreciated!
Tried the code with the specified version and is working correctly. There is an errant ";" in your code (which is also in the docs) that should be removed and may be the culprit. Change the following line to the below.
Hope of help!
dialogArgs: {
isReloaded: true
}
I'm building a bot using gupshup.io using the scripting way ... but handling some things in default.js file as mentioned in the docs
I'm trying in a handler function to check if the event.message equals specific string to go to another section in the script
can anybody please help ?
thanks a lot
So to achieve that you can create a child state to go another section and just set options.next_state to that state. I mean suppose you have a script like this
[main]
inputParser:Welcome to New Bot.
thisFlow:
This is a output of this flow.
callAnotherFlow:
:call default.anotherFlow
[anotherFlow]
This is another flow.[[Wow, No]]
Wow
Thanks
No
Oh!
So in case if the message is 'another flow' you want the second flow to begin. So in the input parser you can create something like.
module.exports.main = {
inputParser: (options, event, context, callback)=>{
if(event.message.toLowerCase() === "another flow"){
options.next_state = 'callAnotherFlow';
}else{
options.next_state = 'thisFlow';
}
callback(options, event, context);
}
}
I think this is what you are looking for.
I'm getting an SSL error thrown by node. This is the exact line of code triggering it:
https://github.com/nodejs/node/blob/master/lib/tls.js#L201
I'm a novice to node and honestly I don't know how to correctly identify this SSL-specific error. In my code, I might look for the Hostname/IP doesn't match certificate's altnames string, but that looks very ugly.
Is there a better way to identify this error, other than looking for text in the error message?
Thanks!
You can handle the known error in a better way by create array of errors and that can also attached with the action need to be taken.
When you handle the exception / error, map them against the array you created using js prototypes and take necessary action.
Hope this will help. Thanks.
Updating...
var arrayOfErros = [
{error:"IP Error",action:"FunctionCall",Message:"You are getting IP Error"},
{error:"IP Error1",action:"FunctionCall1",Message:"You are getting IP Error1"}
]
try {
} catch(exp) {
arrayOfErros.findError(exp,function(msg,action) {
// do eval or show msg or any actions
});
}
Array.findError(err, callback) {
//run through array and return msg and action wn matches.
}