Issue encountered with PouchDB: The error message reads as follows - TypeError: angular.factory is not a

Currently, I am delving into the realm of utilizing PouchDB within an AngularJS environment. My reference point is these guiding instructions located at https://github.com/wspringer/angular-pouchdb

The stumbling block I face pertains to grasping the syntax necessary for crafting factories or services. My progress halts upon reaching the segment on "Interacting with the database"

app.js

'use strict';

angular
  .module('myappApp', [
    'ngCookies',
    'ngResource',
    'ngSanitize',
    'ngRoute',
    'pouchdb'
  ])
  .config(function ($routeProvider) {
    $routeProvider
      .when('/', {
        templateUrl: 'views/main.html',
        controller: 'MainCtrl'
      })
      .otherwise({
        redirectTo: '/'
      });
  })
angular.factory('someservice', function(pouchdb) {
  // Leverage pouchdb capabilities here.
  var db = pouchdb.create('testdb');
  pouchdb.destroy('testdb');
  db.put({_id: 'foo', name: 'bar'});
});

Upon integrating the "db.put", The error message that surfaces in the browser’s console states:

 [15:17:45.343] TypeError: angular.factory is not a function @ http://127.0.0.1:9000/scripts/app.js:21

Answer №1

When it comes to the angular object, there is no factory method available, resulting in it being returned as undefined.

To resolve this issue, try placing it within the object that is returned by the module method.

angular
  .module('myappApp', [
    'ngCookies',
    'ngResource',
    'ngSanitize',
    'ngRoute',
    'pouchdb'
  ])
  .config(function ($routeProvider) {
    $routeProvider
      .when('/', {
        templateUrl: 'views/main.html',
        controller: 'MainCtrl'
      })
      .otherwise({
        redirectTo: '/'
      });
  })
  // Take note of the subtle difference here, utilizing the object returned by module() and config()
  .factory('someservice', function(pouchdb) {
    // Perform actions with pouchdb.
    var db = pouchdb.create('testdb');
    pouchdb.destroy('testdb');
    db.put({_id: 'foo', name: 'bar'});
  });

Answer №2

Furthermore, I successfully modified this code snippet to verify the functionality of the DB http://plnkr.co/edit/M2K7no75zonXKcXKF9Sc?p=preview

app.js 'use strict';

angular
  .module('myappApp', [
    'ngCookies',
    'ngResource',
    'ngSanitize',
    'ngRoute',
    'pouchdb'
  ])
  .config(function ($routeProvider) {
    $routeProvider
      .when('/', {
        templateUrl: 'views/main.html',
        controller: 'MainCtrl'
      })
      .otherwise({
        redirectTo: '/'
      });
  })
  .factory('MyModel', function(pouchdb) {
    return new pouchdb.create('mydb');
  })
  .run(function (){
    var db = new PouchDB('mydb');

    db.info().then(function(info) {
      if (info.doc_count < 2) {
        db.post({display: 'Hello'});
        db.post({display: 'World'});
      }
    });
  });

main.js

'use strict';

angular.module('myappApp')
  .controller('MainCtrl', function ($scope, MyModel) {
    $scope.awesomeThings = [
      'HTML5 Boilerplate',
      'AngularJS',
      'Karma'
    ];
    MyModel.info().then(function(info) {
      $scope.numOfDocs = info.doc_count;
    });

    $scope.options = {
      db: MyModel,
      name: 'Visitor'
    };

    $scope.db = MyModel;
  });

main.html

  <p>Greetings {{options.name}}! There are {{numOfDocs}} documents in your Pouch</p>

  <p>Root scope:</p>
  <ul>
    <li pouch-repeat="item in db" ng-bind="item.display"></li>
  </ul>

  <p>Child scope:</p>
  <ul>
    <li pouch-repeat="item in options.db" ng-bind="item.display"></li>
  </ul>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using the map function in React, you can tie the state to a conditional statement

I am facing an issue with my post list where I want to show comments only when a post is toggled. However, I cannot access the state in my method that checks if the post is toggled or not. Shouldn't arrow functions automatically bind the context? va ...

Problem: Vue 3 build does not generate service worker

After adding the "@vue/cli-plugin-pwa": "^4.5.12" to my package.json and setting up workbox configuration in my vue.config.js, I encountered an issue. When running npm run build:prd with the script vue-cli-service build --mode prod.exam ...

Updating parent array values within child components in React

Currently, I am working on a React application where I need to save the handlers for all windows opened from the app. Previously, before using React, I stored these windows in a global array attached to the parent window, although I understand that using J ...

The 'load()' event method in jQuery does not function properly within the 'ready()' event method on mobile browsers

After testing on Firefox mobile and Chrome mobile browsers, I found that my code works perfectly on computer browsers. Below is the code I have inside the ready() and load() event methods: $(document).ready(function(){ $(window).on('load hashchan ...

What could be causing my GET route to return an empty array?

I've been attempting to retrieve an event by its id, but for some reason, I'm receiving an empty array as a result in Postman. Below is the code snippet of my route: import { events } from '../../../db.json'; const handler = async (re ...

When anchor is set programmatically, the focus outline is not displayed

I am currently facing an issue with my application where I am programmatically setting focus on elements in certain scenarios. While it generally works well, I have noticed that when I set the focus on an anchor element using $("#link1").focus(), the focus ...

eslint is designed to not handle multiple package.json files

There are two package.json files in my project root folder └ app ---- /public └ /styles └ /src └ package.json └ eslintrc.json └ webpack.config.js └ server - /something └ /something ...

How can one transform a web-based application into a seamless full-screen desktop experience on a Mac?

"Which software can be utilized to enable a web application to display an icon on the desktop of a Mac computer, while also opening up the web application in a fully immersive full-screen mode that supports all the touch and gesture functionalities provi ...

Utilizing React JS to assign various state values from a dropdown menu selection

In my project, I have implemented a dropdown list populated from an array of values. This dropdown is linked to a handleSelect function. const handleSelect = (e) => { handleShow() setCommunityName(e) } <DropdownButton id="dropdown-basi ...

Leveraging AngularJS, optimizing SEO, and hosting S3 static pages

My web application utilizes AngularJS for the front-end and .NET for the back-end. Within my application, there is a list view. When each item on the list is clicked, it retrieves a pre-rendered HTML page from an S3 bucket. I am implementing Angular stat ...

Hiding the icon if there are no child elements present

Currently, I am in the process of constructing a TreeView using the Treeview component from Material UI, which can be found at this link. The component I have designed below is responsible for fetching data when a node is expanded. The tree structure is s ...

Unveiling the secrets to retrieving JSON array objects in HTML with the help of Angular!

Looking to display a JSON file in HTML, but struggling with syntax for deeply nested objects. { "questions": [ { "question": "Qw1", "answers": [ { "answers": "An1", "value": 25 }, { ...

Is it true that Safari restricts AJAX Requests following a form submission?

I've developed a JavaScript-based upload progress meter that utilizes the standard multipart submit method instead of submitting files in an iframe. The process involves sending AJAX requests during submission to retrieve the percentage complete of th ...

I prefer to disable the toggle feature if the target remains unchanged

My goal is to create a step-by-step ordering system using the data-id="x" attribute to determine which content should be visible when selected. I want to make sure that if two elements have the same data-id, clicking on one will deselect the other. Any su ...

Guide on accessing Azure Configuration Application settings in a Node application with JavaScript

Currently, I have a node.js application named "myClient" deployed on Azure as an App Service. Within this setup, I utilize several configuration files that contain specific values tailored to their respective runtime environments: appconfig.json is used f ...

"Looking for a way to eliminate the background of an image on NextJS? Learn

https://i.stack.imgur.com/zAsrJ.png When this image is incorporated into a Nextjs rendering, it unexpectedly includes additional content. <div className="flex flex-col items-start justify-start rounded-3xl p-4 bg-boxi w-1/3"> <div cla ...

Socket.io-powered notification system

I'm currently in the process of developing a notification system for my Events Manager Website. Every time a user is logged in and performs an action (such as creating an event), a notification about the event creation should be sent to other user ...

Is the this.props.onChange method called within the render function?

I'm having trouble grasping the concept of the assigned onChange property in this TextField component I found in the material-ui library: <TextField style = {{"padding":"10px","width":"100%"}} type = {'number'} valu ...

The Route.go function is ineffective when used in form submission

Hey there! I'm new to the world of meteor and javascript, currently working on a fun little app project. My tool of choice is meteor with ionic. Here's a snippet from my simple form: <form> .... <label class="item item- ...

Is there a way to trigger a JavaScript function once AJAX finishes loading content?

I've been utilizing some code for implementing infinite scrolling on a Tumblr blog through AJAX, and it's been performing well except for the loading of specific Flash content. The script for the infinite scroll can be found here. To address the ...