Does anybody know why my code below always returns undefined variable in the callback. From the api documentation it says that it is stable in chrome 34, I have already updated my chrome into chrome 34 but still I get undefined value.
chrome.desktopCapture.chooseDesktopMedia(
["screen", "window"],
function (streamId) {
console.log(streamId); //always returns undefined.
});
By the way I am using Ubuntu 32-bit with chrome version 34.0.1847.132
Chrome always fires the callback, even when it has an exception. You have to check for chrome.runtime.lastError:
chrome.desktopCapture.chooseDesktopMedia(
["screen", "window"],
streamId => {
if(chrome.runtime.lastError)
console.error(chrome.runtime.lastError);
else
console.log(streamId);
});
Alternatively you can use an asynchronous/Promise wrapper library like chrome-extension-async so that you can catch the await exception:
try {
streamId = await chrome.desktopCapture.chooseDesktopMedia(["screen", "window"]);
console.log(streamId);
}
catch(err) {
console.error(err);
}
Related
Can somebody PLEASE tell me what I'm doing wrong here :
I'm sorry, I feel like I'm losing my mind for 30 minutes.
getChartData() {
this.http
.get('http://localhost:3001/transactions/' + sessionStorage.getItem('id'))
.toPromise()
.then((data: any) => {
this.data = data;
});
}
<button (click)="getChartData()">click</button>
Error :
TypeError: undefined is not an object (evaluating 'this.http.get')
You have to return something from your function. With your current code example, If you hover your mouse over the function name, you'll see it says void.
Also, I recommend using observables rather than promises, Angular uses heavily
getChartData() {
// return the HTTP response
return this.http.get(
'http://localhost:3001/transactions/'
+ sessionStorage.getItem('id'))
}
Then simply, subscribe to it to get the response back
someFunction() {
this.service.getChartData().subscribe((response) => console.log(response))
}
I have tried to get a result but I can't find how to solve SyntaxError: await is only valid in async function
static async searchYoutube(query, message, voiceChannel) {
await youtube.searchVideos(query, 5).catch(videos => {
await message.say(
':x: There was a problem searching the video you requested!'
);
return;
});
if (videos.length < 5 || !videos) {
message.say(
`:x: I had some trouble finding what you were looking for, please try again or be more specific.`
);
return;
}
Now, they also recommended color async on the front, I did it but it marks another error
TypeError: Cannot read property 'length' of undefined
static async searchYoutube(query, message, voiceChannel) {
await youtube.searchVideos(query, 5).catch(async (videos) => {
await message.say(
':x: There was a problem searching the video you requested!'
);
return;
});
if (videos.length < 5 || !videos) {
message.say(
`:x: I had some trouble finding what you were looking for, please try again or be more specific.`
);
return;
}
Ad SyntaxError: await is only valid in async function – this is pretty much self explanatory error, however, there's no need to await for message.say(...) in either of these cases.
Ad TypeError: Cannot read property 'length' of undefined – this is because videos on line 8 is not defined anywhere but inside (and only inside of) that .catch block.
I'd recommend using .then block same way you use .catch in order to process searchVideos results. The code is asynchronous (is run concurrently), but the wrapper function itself doesn't need to be async.
static searchYoutube(query, message, voiceChannel) {
youtube.searchVideos(query, 5)
.then(videos => {
if (!videos || videos.length < 5) // first check !videos, then videos.length
message.say(`:x: I had some trouble finding what you were looking for, please try again or be more specific.`)
else {
... // continue your code
}
})
.catch(error => { // catch gives an error, not the result
message.say(':x: There was a problem searching the video you requested!')
})
}
I am developing a NodeJS application. I had a problem saving an object to the database; the code in my controller was:
console.log("Before save: " + transaction);
transaction = await transaction.save();
console.log("After save");
In the terminal running the Node application, I saw:
Before save: {
_id: ...,
created: ...,
...
}
and nothing more, while the browser page looks like it's loading.
I tried wrapping it in a try-catch block:
try {
transaction = await transaction.save();
} catch (err) {
console.log(err);
}
and I get the same output in the terminal. I suppose that since I don't see the error in the console without the try-catch, I don't see it with it either.
One problem was that the MongoDB collection for that object did not exist (and I asked about it here). But I still have another error.
How can I enable MongoDB to show those errors (missing collection, and the error I am still looking for) in the terminal running NodeJS while I'm in development?
I found the problem: I had omitted a next(); in a pre-save hook:
SomeSchema.pre('save', function(next) {
// any logic you require
if (this.condition) {
// ...
}
next(); // <- this is required, else Mongo won't save nor return
});
So I was not getting an error because there was no error, only a break in the flow of calls.
I am using knex npm version 0.15.2. while Rollback the transaction I'm getting the following error:
Error: Transaction rejected with non-error: undefined
Trx.rollback()
above function used for rollback.
Same code working for knex version 0.12.6
This is the function I used for commit/Rollback.
function Commit(pTrx, pIsCommit, pCallback) {
try {
var co = require("co");
var q = require('q');
var Q = q.defer();
co(function* () {
if (pIsCommit) {
yield pTrx.commit();
} else {
yield pTrx.rollback();
}
Q.resolve(pCallback('SUCCESS'));
}).catch(function (error) {
Q.reject(pCallback(error));
});
return Q.promise;
} catch (error) {
console.log(error)
}
}
This code could use some work. :) Here are a few things that pop out:
You don't need co or q anymore. Promises and async/await are built-in and much simpler to use. Async functions automatically return promises that will be resolved with the value returned or rejected if an error is thrown. Learn about async/await here: https://jsao.io/2017/07/how-to-get-use-and-close-a-db-connection-using-async-functions/
You shouldn't return success as a string. If the function completes without throwing an exception, then success is implied. I see people do this from time to time, but it's often for the wrong reasons.
You shouldn't accept callback in a function that returns a promise, it should be one or the other. The caller of your function will either pass a callback or await it's completion.
If you are going to use callbacks, then you should return null as the first parameter when successful. See the last sentence here: https://nodejs.org/en/knowledge/getting-started/control-flow/what-are-callbacks/
Your function name, Commit, starts with an uppercase. This convention is typically used to indicate that the function is a contstructor function and meant to be invoked with the new keyword.
Here's how the function could look once cleaned up:
async function commit(pTrx, pIsCommit) {
// Not using a try/catch. If an error is thrown the promise returned will be rejected.
if (pIsCommit) {
await pTrx.commit();
} else {
await pTrx.rollback();
}
// Not going to return anything. If we get to this point then success is implied when the promise is resolved.
}
A consumer of your function would call it with something like:
async function myWork() {
// do work; get pTrx
try {
await commit(pTrx, true);
// If I get here, then I can assume commit was successful, no need to check a return value of 'SUCCESS'
} catch (err) {
// handle error
}
}
It's hard to say where the issue is with the code in its current state. However, if the issue truly is with Knex, then you should probably post this as an issue in the Knex repo: https://github.com/tgriesser/knex/issues But you should write a reproducible test case that proves its an issue with Knex.
I am trying to use Argon2 encryption in Node, but when I try to encrypt a string, I get this error:
Cannot read property 'catch' of undefined
I have tried handling the errors from the promise returned by the argon2.hash function, but it still does not work.
This is my code so far:
argon2.hash('password', {type: argon2.argon2id})
.then(hash => {
// do something with the hash
}).catch(err => {
// Handle the error
});
Could anyone please help me with fixing this error?
In my case I got that error message because I
a) spied on some async method
spyOn(sut,'myAsyncMethod')
b) later appended .catch() to the original method call and forgot to extend the spy to return a value/promise.
Returning a promise from the spy solved my issue:
spyOn(sut,'myAsyncMethod').and.returnValue(new Promise(resolve=>resolve()));
It throws an exception, it does not return a promise. As such, there is no promise object on which the then(…).catch(…) methods could be invoked.
To catch it, you would need an actual try/catch block
from argon2 github page, you should do this:
const argon2 = require('argon2');
try {
const hash = await argon2.hash("password");
} catch (err) {
//...
}
Try the following instead:
argon2.hash('password', {type: argon2.argon2id})
.then(hash => {
// do something with the hash
}, err => {
// Handle the error
});
The second parameter to a then clause is the onError handler.