Month: December 2019

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.