convertFromRaw giving this error "Invariant Violation: invalid RawDraftContentState" - html

I want to convert html code to draft object to save my editorState
But convertFromRaw function giving me Invariant Violation: invalid RawDraftContentState <- this error.
try {
const htmlToDraftObject = htmlToDraft(
post.content
);
console.log(htmlToDraftObject);
console.log(convertFromRaw(htmlToDraftObject));
} catch (error) {
console.log(error, "error");
}
console.log(htmlToDraftObject);
this line is returns this object
{"contentBlocks":[{"key":"apdo3","type":"unstyled","text":"\"\\n","characterList":[{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null}],"depth":0,"data":{}},{"key":"2lgc","type":"unstyled","text":"","characterList":[],"depth":0,"data":{}},{"key":"118p1","type":"atomic","text":" \\n","characterList":[{"style":[],"entity":"5526de6f-06fd-445d-9ae4-e5bae145adab"},{"style":[],"entity":null},{"style":[],"entity":null}],"depth":0,"data":{}},{"key":"13tke","type":"unstyled","text":"\\n\"","characterList":[{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null}],"depth":0,"data":{}}],"entityMap":{"5526de6f-06fd-445d-9ae4-e5bae145adab":{"type":"IMAGE","mutability":"MUTABLE","data":{"src":"\\\"https://p.bigstockphoto.com/GeFvQkBbSLaMdpKXF1Zv_bigstock-Aerial-View-Of-Blue-Lakes-And--227291596.jpg\\\"","alt":"\\\"undefined\\\"","height":"","width":""}}}}
post.content is a simple html code
I tried it without htmlToDraft function too .But it gave me another error
what am I doing wrong?

Related

Is there a way to have Nest use the ExceptionsHandler error message as the API response message when an exception is thrown?

When an error is thrown within my Nest API, often times the error that is thrown is not an HttpException, and therefore the standard Nest API response that I see from the client's side is:
{
"statusCode": 500,
"message": "Internal server error"
}
But when I look at the console log, there is a much more descriptive error message, for example:
[Nest] 18476 - 06/23/2022, 2:28:07 PM ERROR [ExceptionsHandler] Cannot perform update query
because update values are not defined.
UpdateValuesMissingError: Cannot perform update query because update values are not defined.
Is there a way I can route this message to be the in the response body of the response given back to the API client?
You can use Global Filter in NestJs to catch error and then throw a good error.
For exemple, you can create a Exception Filter
// UpdateValuesMissingError.exception-filter.ts
import { ArgumentsHost, Catch, ExceptionFilter } from '#nestjs/common';
import { Request, Response } from 'express';
#Catch(UpdateValuesMissingError)
export class UpdateValuesMissingErrorFilter implements ExceptionFilter {
catch(exception: UpdateValuesMissingError, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
const request = ctx.getRequest<Request>();
console.log(exception);
response.status(400).json({
statusCode: 400,
timestamp: new Date().toISOString(),
path: request.url,
});
}
}
and in your main.ts add this
app.useGlobalFilters(new HttpExceptionFilter());
And now in response.status(400).json({}) you can add everything you want in your body. Like the error message of Axios.

react native- json parsing (cant parse json object in my code)

Here is my code. In this I got x but when I try to parsing x for getting object of x 'isloggedin' it gives error. what should I do wrong tell me if you understand my problem.
componentDidMount() {
this.onLoad();
}
onLoad = async () => {
try {
var walletdata = await AsyncStorage.getItem('wallet');
this.setState({x: JSON.parse(walletdata)})
this.setState({y: JSON.stringify(this.state.x)})
console.log("output: "+ this.state.y);
console.log("output y:"+JSON.parse(this.state.y.isloggedIn));
}catch(error){
console.log("error: "+error);
}
}
error: SyntaxError: JSON Parse error: Unexpected identifier "undefined"
Probably is because 'wallet' key has undefined value in the caché storage. You should check first the value (right after getting the item from the storage) and if it's undefined then you have to set it first.
Probably you need to wait for the first setState function to finish, So that you can get access to it in second call.
In short, It will take some time for setting your value in State and you are trying to access it before updating the state. So try to access the values in the callback of setState function.
Instead of this,
this.setState({x: JSON.parse(walletdata)})
this.setState({y: JSON.stringify(this.state.x)})
console.log("output: "+ this.state.y);
console.log("output y:"+JSON.parse(this.state.y.isloggedIn));
You can use this:
this.setState({x: JSON.parse(walletdata),y: JSON.stringify(walletdata.x)},()=>{
console.log("output: "+ this.state.y);
console.log("output y:"+JSON.parse(this.state.y.isloggedIn));
});
I have solved my problem. Thank you for your answers. below is how could I solved my question.
try {
var walletdata = await AsyncStorage.getItem('wallet');
this.setState({wallet: JSON.stringify(JSON.parse(walletdata))})
this.setState({isLoggedin:JSON.parse
(this.state.wallet).isLoggedin});
console.log(this.state.isLoggedin);
}catch(error){
console.log("error: "+error);
}

Why am I getting an undefined value when calling these scripts in Google App Maker?

I do not understand why when I am calling a ServerScript method from a ClientScript method, I am getting a value of undefined.
ClientScript:
function clientScript() {
var message;
message = google.script.run.test();
console.log("Message: " + message);
}
ServerScript:
function serverScript() {
return "hello";
}
I expected the console to print: Message: hello. However, I am getting this printed to my console: Message: undefined. Why am I getting an undefined value in my ClientScript method when I am returning a defined value in my ServerScript method? Thank you!
Because server calls are asynchronous. In order to handle server response you need to pass callback. Here is a snippet from Apps Script docs:
function onSuccess(numUnread) {
console.log(numUnread);
}
google.script.run.withSuccessHandler(onSuccess)
.getUnreadEmails();
Just in case AMs docs interpretation of the same thing - https://developers.google.com/appmaker/scripting/client#call_a_server_script

Why as3 giving me syntax error in a for loop?

I'm new with actionscript and I keep getting syntax error with the following for loop:
for each (target:Target in targets) {
if(target != null) {
target.parent.removeChild(target);
}
}
And I got this error message:
Syntax error: expecting in before colon.
What is the problem?
You forgot to declare the variable, it should be:
for each (var target:Target in targets) {
// …
}
Note the var.

Hel­p with actionscript 3 syntax

I would like your help here, this is my actionscript3 code.
Everytime I compile I get error.
Line 1537 1073: Syntax error: expecting a catch or a finally clause.
This is the code
{
try
{
}
_loc_3.badge.gotoAndStop(param1.split(":")[2]);
}
And the second error
{
try
{
}
_loc_3.badge.gotoAndStop(param1.split(":")[2]);
}
The second error says:
Line 1537 1084: Syntax error: expecting rightbrace before semicolon.
Could anyone help me here, thanks in advance.
UPDATE: After I add a right brace before the semicolon it gives more errors.
The first error is really explicit, you need a catch block. The empty try block is useless.
The syntax found on a website.
try {
//Your code
}
catch(e:Error) { // Error handling
trace("Error found: " + e);
}
//Optional
finally {
// Closing connection for example.
}
Website reference in french
A try cannot be used without a catch. the idea there is lets try this piece of code and if we run into any problems stop and execute the content of whatever is in the catch. the finally is used for executing code that you want to run regardless of whether the try or catch gets executed.
in the first error:
you are just missing a catch. also maybe include some code into the try statement, otherwise its pointless to use.
example:
try
{
//try to feed my dog
feedDog();
}
//if some error occurs in feedDog() then the catch will be called
//if you want to catch specific exceptions
//then specify its type instead of Exception
catch (Exception e)
{
trace("unable to feed dog");
}
//this will execute whether the dog was fed successfully or not
finally
{
trace("leave");
}
with the second error: you are probably missing a '}' somewhere in that function. Indent your code so that these will become clearly visible to you and you can match every '{' with its corresponding '}'