Latest Event Updates

React inline styles – merge two style objects

Posted on Updated on

I had two style objects that I needed to merge for a component. One was the style objects that makes the component look good, the other was to set the display visible or not.

I could get around it by wrapping the component in another div that would get the display style, but here is how to minimize your divs

const style1 = {color: ‘blue’}
const style2 = {display: ‘none’} //used with state that toggles visibility

<div style={Object.assign(style2, style1)} > </div>

The reason the display style should be first is because the key pairs from the second object are copied to the first. so style2 would now be

{display: ‘none’, color: ‘blue’}

 

Node Error : Promise.all is not a function

Posted on

Are you getting the message?

Promise.all is not a function

At first I was thinking is my node version out of date?

But the answer is actually that your Promise.all function inst receiving an array. 

 

 

axios – on POST errors JSON response from api server not being printed

Posted on

When you POST from curl, you are getting the JSON response from the server that you setup.

On your frontend when trying to print the error to the console it’s not coming up. Instead you get the following:

xhr.js:166 POST MYURLHERE 400 (Bad Request)

Even if you print the whole error or JSON stringify the error you aren’t seeing the response

ANSWER

make sure you are printing error.response.data 

 

mongoose-unique-validator not working?

Posted on Updated on

Are you using the mongoose-unique-validator package and not getting any errors when you post duplicate objects?

CURRENT

const personSchema = new mongoose.Schema({name:String,number: String})
personSchema.plugin(uniqueValidator)

SOLUTION

You need to set a unique attribute on the key you want uniqueness with. By default mongo only adds uniqueness to the __id key

const personSchema = new mongoose.Schema({name: {type:String, unique: true}, number: String})

SyntaxError: Unexpected token c in JSON at position 1 when you POST JSON data with curl

Posted on Updated on

When trying to use curl to POST data to your api and getting an error?

The error:

SyntaxError: Unexpected token c in JSON at position 1

Answer

You need to wrap your json block in quotes

curl -X POST -H "Content-Type: application/json" -d '{"content":"a"}' your_url_here

“Invalid Host Header” Errors when running your React project off your cloud ide

Posted on

This one was a pain. So your react app is hosted on localhost:3000 and your node backend is on localhost:3001. This isn’t a problem if you a developing locally. You open up your browser and everything connects perfectly. But if you are developing on a cloud ide or vps, you are probably connecting to something like someweirdname.cloudidename.com.

By itself, running npm start will give you your project nicely. But once you have a backend that your browser isnt accessing locally, you might get the dreaded  “Invalid Host Header”

Here is how I fixed my problem:

File: package.json  add the following:  where the value is your ide host name

"proxy": "http://mybackend.mycloudidehost.c9.io"

.env.development.local <- thats DOT ENV DOT DEVELOPMENT DOT LOCAL , I spent a good while troubleshooting because of the missing DOT,  add the following:

DANGEROUSLY_DISABLE_HOST_CHECK=true

The above is not recommended if you are developing locally, but its a cloud ide.

 

Heroku : Application error : error code=H12 desc=”Request timeout” : when using remote database

Posted on Updated on

I was working with a Node application I built, which connected to a hosted MongoDB database. Everything worked perfectly on my development machine. Once I pushed everything to heroku is where I would start getting problems.

I received this error after trying to connect to my apps webpage

Application error

An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details. You can do this from the Heroku CLI with the command
heroku logs --tail

When checking the logs this error stood out

2019-08-01T18:36:14.968797+00:00 heroku[router]: at=error code=H12 desc=”Request timeout” method=GET path=”/api/notes” host=spongebobs-basin
-47509e646.herokuapp.com request_id=e70f60dc-321d-421c-9c24-6974ba3a79bb fwd=”99.99.99.9″ dyno=web.1 connect=0ms service=30000ms status=503 b
ytes=0 protocol=https

So what are the important things in this line?

  • desc=”Request timeout”
  • method=GET path=”/api/notes”
  • service=30000ms
  • status=503 b

I am connecting to the correct path, using the correct method, but after 30 seconds the server is timing out , so heroku sends a 503 Service Unavailable.

This was a strange one to figure out since locally everything worked great. I changed some things around for testing and found that the application on heroku was not connecting to the remote mongoDB host. Locally it worked great. Was this a herkoku problem?

Solution:

The .env file I used locally required the mongoDB URI to be in quotes. The app would not work without the quotes. But the configuration variables in heroku needed to be added without the quotes.

  • .env  ‘mongodb+srv://username:password@somecluster.mongodb.net/note-app’
  • Config Vars  mongodb+srv://username:password@somecluster.mongodb.net/note-app