gpt4 book ai didi

javascript - PouchDB : TypeError: angular. 工厂错误不是函数

转载 作者:行者123 更新时间:2023-12-02 17:21:09 24 4
gpt4 key购买 nike

我正在尝试弄清楚如何在 AngularJS 中使用 PouchDB。我正在尝试遵循这些说明 https://github.com/wspringer/angular-pouchdb

我认为我在理解创建工厂和/或服务的语法时遇到问题。我已经了解了“与数据库交互”部分

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) {
// Do something with pouchdb.
var db = pouchdb.create('testdb');
pouchdb.destroy('testdb');
db.put({_id: 'foo', name: 'bar'});
});

当我添加“db.put”时,我在浏览器控制台中看到的消息是:

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

最佳答案

Angular 对象没有工厂方法,因此它将返回为未定义。

尝试将其放在模块方法返回的对象上。

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

关于javascript - PouchDB : TypeError: angular. 工厂错误不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23941395/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com