Knex.js - TypeError: db(…).return(…).insert is not a function












0















I am getting the error : TypeError: db(...).return(...).insert is not a function



For some reason the code runs 'db.select('*')' without a problem but when it gets to



'db('courses')
.return('*')
.insert({'


it gives the error : TypeError: db(...).return(...).insert is not a function.



I can not figure out what is going on, I've been stuck on this for 4 hours.



My code sample :



const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const knex = require('knex');


const db = knex({
client:'pg',
connection:{
host:'127.0.0.1',
port:'5432',
user:'postgres',
password:'',
database:'YouList'
}
})


const app = express();

app.use(bodyParser.json());
app.use(cors())

db.select('*').from('users').then(data => {
console.log(data,'oia a data ai');
});

/*testando sem API*/
db('courses')
.return('*')
.insert({
title: "o grande curso",
description : "uma introducao aos ventos do leste",
user_id : 78,
urlimage : "https://i.imgur.com/TiavcG9.jpg",
created : new Date()
})
.then(console.log)
.then(user => {
res.json(user[0]);
})
.catch(err => res.status(400).json('unable to register'))









share|improve this question



























    0















    I am getting the error : TypeError: db(...).return(...).insert is not a function



    For some reason the code runs 'db.select('*')' without a problem but when it gets to



    'db('courses')
    .return('*')
    .insert({'


    it gives the error : TypeError: db(...).return(...).insert is not a function.



    I can not figure out what is going on, I've been stuck on this for 4 hours.



    My code sample :



    const express = require('express');
    const bodyParser = require('body-parser');
    const cors = require('cors');
    const knex = require('knex');


    const db = knex({
    client:'pg',
    connection:{
    host:'127.0.0.1',
    port:'5432',
    user:'postgres',
    password:'',
    database:'YouList'
    }
    })


    const app = express();

    app.use(bodyParser.json());
    app.use(cors())

    db.select('*').from('users').then(data => {
    console.log(data,'oia a data ai');
    });

    /*testando sem API*/
    db('courses')
    .return('*')
    .insert({
    title: "o grande curso",
    description : "uma introducao aos ventos do leste",
    user_id : 78,
    urlimage : "https://i.imgur.com/TiavcG9.jpg",
    created : new Date()
    })
    .then(console.log)
    .then(user => {
    res.json(user[0]);
    })
    .catch(err => res.status(400).json('unable to register'))









    share|improve this question

























      0












      0








      0








      I am getting the error : TypeError: db(...).return(...).insert is not a function



      For some reason the code runs 'db.select('*')' without a problem but when it gets to



      'db('courses')
      .return('*')
      .insert({'


      it gives the error : TypeError: db(...).return(...).insert is not a function.



      I can not figure out what is going on, I've been stuck on this for 4 hours.



      My code sample :



      const express = require('express');
      const bodyParser = require('body-parser');
      const cors = require('cors');
      const knex = require('knex');


      const db = knex({
      client:'pg',
      connection:{
      host:'127.0.0.1',
      port:'5432',
      user:'postgres',
      password:'',
      database:'YouList'
      }
      })


      const app = express();

      app.use(bodyParser.json());
      app.use(cors())

      db.select('*').from('users').then(data => {
      console.log(data,'oia a data ai');
      });

      /*testando sem API*/
      db('courses')
      .return('*')
      .insert({
      title: "o grande curso",
      description : "uma introducao aos ventos do leste",
      user_id : 78,
      urlimage : "https://i.imgur.com/TiavcG9.jpg",
      created : new Date()
      })
      .then(console.log)
      .then(user => {
      res.json(user[0]);
      })
      .catch(err => res.status(400).json('unable to register'))









      share|improve this question














      I am getting the error : TypeError: db(...).return(...).insert is not a function



      For some reason the code runs 'db.select('*')' without a problem but when it gets to



      'db('courses')
      .return('*')
      .insert({'


      it gives the error : TypeError: db(...).return(...).insert is not a function.



      I can not figure out what is going on, I've been stuck on this for 4 hours.



      My code sample :



      const express = require('express');
      const bodyParser = require('body-parser');
      const cors = require('cors');
      const knex = require('knex');


      const db = knex({
      client:'pg',
      connection:{
      host:'127.0.0.1',
      port:'5432',
      user:'postgres',
      password:'',
      database:'YouList'
      }
      })


      const app = express();

      app.use(bodyParser.json());
      app.use(cors())

      db.select('*').from('users').then(data => {
      console.log(data,'oia a data ai');
      });

      /*testando sem API*/
      db('courses')
      .return('*')
      .insert({
      title: "o grande curso",
      description : "uma introducao aos ventos do leste",
      user_id : 78,
      urlimage : "https://i.imgur.com/TiavcG9.jpg",
      created : new Date()
      })
      .then(console.log)
      .then(user => {
      res.json(user[0]);
      })
      .catch(err => res.status(400).json('unable to register'))






      node.js express knex.js






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 23:05









      Plínio HelpaPlínio Helpa

      535




      535
























          1 Answer
          1






          active

          oldest

          votes


















          2














          You are calling return and not returning. You should also chain it off of insert, rather than having it before. Makes it a bit more idiomatic.



          So just switch it around...



          db('courses')      
          .insert({
          foo: 'bar'
          })
          .returning('*')





          share|improve this answer
























          • great tip my friend!

            – Plínio Helpa
            Nov 22 '18 at 18:06











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421687%2fknex-js-typeerror-db-return-insert-is-not-a-function%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          You are calling return and not returning. You should also chain it off of insert, rather than having it before. Makes it a bit more idiomatic.



          So just switch it around...



          db('courses')      
          .insert({
          foo: 'bar'
          })
          .returning('*')





          share|improve this answer
























          • great tip my friend!

            – Plínio Helpa
            Nov 22 '18 at 18:06
















          2














          You are calling return and not returning. You should also chain it off of insert, rather than having it before. Makes it a bit more idiomatic.



          So just switch it around...



          db('courses')      
          .insert({
          foo: 'bar'
          })
          .returning('*')





          share|improve this answer
























          • great tip my friend!

            – Plínio Helpa
            Nov 22 '18 at 18:06














          2












          2








          2







          You are calling return and not returning. You should also chain it off of insert, rather than having it before. Makes it a bit more idiomatic.



          So just switch it around...



          db('courses')      
          .insert({
          foo: 'bar'
          })
          .returning('*')





          share|improve this answer













          You are calling return and not returning. You should also chain it off of insert, rather than having it before. Makes it a bit more idiomatic.



          So just switch it around...



          db('courses')      
          .insert({
          foo: 'bar'
          })
          .returning('*')






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 23:31









          dvsoukupdvsoukup

          1,057923




          1,057923













          • great tip my friend!

            – Plínio Helpa
            Nov 22 '18 at 18:06



















          • great tip my friend!

            – Plínio Helpa
            Nov 22 '18 at 18:06

















          great tip my friend!

          – Plínio Helpa
          Nov 22 '18 at 18:06





          great tip my friend!

          – Plínio Helpa
          Nov 22 '18 at 18:06


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421687%2fknex-js-typeerror-db-return-insert-is-not-a-function%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'