Programming

TypeError: Cannot read property ‘search’ of undefined

Posted on Updated on

In a react componenet, I was having a problem calling a function from another function. I kept getting the error


TypeError: Cannot read property 'search' of undefined

by binding “this” to the first function, it was then able to call the second function.

This example by Rafi Ud Daula Refat on Stackoverflow helped me figure this out.

Here is a little example using some code I made for my project

import React, { Component } from 'react';
class Header extends Component {
search() {
console.log("Enter Button Pressed");
}
handleKeyPress(target) {
if(target.charCode==13){
this.search();
}
}
render(){
return(
 input  onKeyPress={this.handleKeyPress.bind(this)} placeholder="Enter search term" 
)
}

*note input not in <> because wordpress doesnt render it correct, keeps making actual inputs or removes the text

Print something x number of times in JSX / ReactJS

Posted on

If you are using ReactJS you might be asking how to print something x number of times.

plain characters there are many ways to do this such as


let stars = "*".repeat(this.props.review.rating) //where rating is 5

this doesn’t work in when you start using elements as the subject of repetition

for the code:

let newstars = .repeat(this.props.review.rating)

this is the error

TypeError: __WEBPACK_IMPORTED_MODULE_0_react___default.a.createElement(…).repeat is not a function

The fix is to add items the following way:


let newstars = []
for(let i=0;i<this.props.review.rating;i++){
stars.push()
}

//now display the JSX as such in your return
{newstars}

Bypass authentication for signup page in Rails API

Posted on Updated on

I added authentication on my rails api.

Works great, only authenticated users can now call on the api.

Now my problem was that I am unable to signup, since it requires authentication. Catch-22.

how to bypass?

Authentication is being inherited from application_controller.rb

for the signup controller, I just inherited directly from ActionController::API

 

 

This was helpful if you need to setup the api and you need assistance.

How do you paste source code into wordpress?

Posted on Updated on

For sometime, I was posting source code as plain text, I knew there was a better way.

just wrap your code as such between code language tags as such:

Screen Shot 2018-05-22 at 9.40.01 AM

Supported formats are :

  • actionscript3
  • bash
  • clojure
  • coldfusion
  • cpp
  • csharp
  • css
  • delphi
  • diff
  • erlang
  • fsharp
  • go
  • groovy
  • html
  • java
  • javafx
  • javascript
  • latex (you can also render LaTeX)
  • matlab (keywords only)
  • objc
  • perl
  • php
  • powershell
  • python
  • r
  • ruby
  • scala
  • sql
  • text
  • vb
  • xml

Looks like wordpress has a nice support page up about it

Rails: stop ActiveRecord::RecordNotFound from breaking app

Posted on Updated on

Was building a rails api backend, but when I would search for a record that was not found, the app would show the error. That is great for testing, but I wanted to handle the error without a 404 since this is an api and wanted to send a specific json message.

Found the solution as such:

class YourController < ActionController::Base

  rescue_from ActiveRecord::RecordNotFound, with: :dude_wheres_my_record

  def show
    # your original code without the begin and rescue
  end

  def dude_where_my_record
    # special handling here
  end
end

Found on stack overflow thanks to noodl

 

 

Drop a database from postgres

Posted on Updated on

in bash

type psql

\list or \l to list your database

terminate any connections by using the following command. example if databse named YourDatabase

select pg_terminate_backend(pid) from pg_stat_activity where datname=’YourDatabase’;

*use procid instead of pid for older versions of postgres

DROP DATABASE “YourDatabase”;

Now your database is gone.

https://stackoverflow.com/questions/7073773/drop-postgresql-database-through-command-line for reference

Cannot push commit to github :

Posted on

Error:

hint: Updates were rejected because the remote contains work that you do

hint: not have locally. This is usually caused by another repository pushing

hint: to the same ref. You may want to first integrate the remote changes

hint: (e.g., ‘git pull …’) before pushing again.

hint: See the ‘Note about fast-forwards’ in ‘git push –help’ for details.

hint: Updates were rejected because the remote contains work that you do

FIX

git push -f origin master

Notes:
forcing an update isn’t ideal but all the commits were on my local machine and I am the only one with access to the repository. Possible the error occurred due to network issue earlier?  Else I would git pull, and then go through the diffs

error: src refspec master does not match any.

Posted on

Started a new react app and wanted to get it on github

Got this error when  I ran

//git push -u origin master

error: src refspec master does not match any.

error: failed to push some refs to “repo address”

what was the problem?

I didnt have any commits!

one commit and push later all is well

Render files and images for Rails 5 api only

Posted on

I had built a rails 5 api only app. By installing the app only version of rails, I lost the view layer of rails. No app/assets, or public files.

This is of course the intended design for an api application but my projects also needed to reference locally stored images. How to overcome?

Example: I need to fetch http://mysite/images/image5.jpg

Solution:

#config/routes.rb . #add a route

get ‘images/:id’, :to => ‘images#show’

#app/controllers/images_controller.rb #create this file, make class ImagesController,

#add method below

def show
id = params[:id]  #this will get the filename
send_file Rails.root.join(“public”, “#{id}.jpg”), type: “image/gif”, disposition: “inline” .          #send the file requested, files stored in public
end

 

This is a word around and there could (probably is) a better way, but it worked to get the project going. During refactor, we can change this and look for a better solution of need be. This was better than having to start a new rails5 app from scratch.

Uncaught (in promise) SyntaxError: Unexpected end of JSON input

Posted on

getting this error when using fetch to connect to my rails api

fetch(`/products/${dataid}/description`)
.then(res=>res.json())
.then(json=>console.log(json))

 

in the browser I was able to get to the page

http://127.0.0.1:3000/products/11/description

 

answer

 

rails action was rendering basic text not json, so I had to change fetch

***Rails

def description
product=Product.find(params[:id])
if product
if product.description
render plain: product.description
else
render plain: “No description”
end end end

***JS

fetch(`/products/${dataid}/description`)
.then(res=>res.text()) //<————————————-
.then(json=>console.log(json))