Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度提供程序在转换为coffee脚本后引发错误_Javascript_Angularjs_Coffeescript_Ionic_Angularjs Provider - Fatal编程技术网

Javascript 角度提供程序在转换为coffee脚本后引发错误

Javascript 角度提供程序在转换为coffee脚本后引发错误,javascript,angularjs,coffeescript,ionic,angularjs-provider,Javascript,Angularjs,Coffeescript,Ionic,Angularjs Provider,我做了一笔非常好的回购 并开始将应用程序翻译成咖啡脚本。几乎所有的工作都很好,但与文件: 我将其转换为: angular.module 'app' .provider 'ParseUtils', -> credentials = applicationId: null restApiKey: null this.initialize = (applicationId, restApiKey) -> credentials.applicationI

我做了一笔非常好的回购

并开始将应用程序翻译成咖啡脚本。几乎所有的工作都很好,但与文件:

我将其转换为:

angular.module 'app'

.provider 'ParseUtils', ->
  credentials =
    applicationId: null
    restApiKey: null

  this.initialize = (applicationId, restApiKey) ->
    credentials.applicationId = applicationId
    credentials.restApiKey = restApiKey

  this.$get = ($http, $q, CrudRestUtils, Utils) ->
    service =
      createCrud: createCrud
      createUserCrud: createUserCrud
      signup: signup
      login: login
      loginOAuth: loginOAuth
      passwordRecover: passwordRecover
      toGeoPoint: toGeoPoint
      toPointer: toPointer
      toDate: toDate
    parseUrl = 'https://api.parse.com/1'
    parseObjectKey = 'objectId'

    getParseData = (result) ->
      if result and result.data
        if !result.data[parseObjectKey] and result.data.results
          return result.data.results
        else
          return result.data


    parseHttpConfig = headers:
      'X-Parse-Application-Id': credentials.applicationId
      'X-Parse-REST-API-Key': credentials.restApiKey

    createCrud = (objectClass, _processBreforeSave, _useCache) ->
      endpointUrl = parseUrl + '/classes/' + objectClass
      service = CrudRestUtils.createCrud(endpointUrl, parseObjectKey, getParseData, _processBreforeSave, _useCache,
        parseHttpConfig)

      service.savePartial = (objectToSave, dataToUpdate) ->
        objectId = if typeof objectToSave == 'string' then objectToSave else objectToSave[parseObjectKey]
        toUpdate = angular.copy(dataToUpdate)
        toUpdate[parseObjectKey] = objectId
        service.save toUpdate

      service

    createUserCrud = (sessionToken, _processBreforeSave, _useCache) ->
      endpointUrl = parseUrl + '/users'
      parseUserHttpConfig = angular.copy(parseHttpConfig)
      parseUserHttpConfig.headers['X-Parse-Session-Token'] = sessionToken

      _processBreforeSaveReal = (user) ->
        delete user.emailVerified
        if _processBreforeSave
          _processBreforeSave user


      service = CrudRestUtils.createCrud(endpointUrl, parseObjectKey, getParseData, _processBreforeSaveReal, _useCache,
        parseUserHttpConfig)

      service.savePartial = (objectToSave, dataToUpdate) ->
        objectId = if typeof objectToSave == 'string' then objectToSave else objectToSave[parseObjectKey]
        toUpdate = angular.copy(dataToUpdate)
        toUpdate[parseObjectKey] = objectId
        service.save toUpdate

      return service

    # user MUST have fields 'username' and 'password'. The first one should be unique, application wise.

    signup = (user) ->
      if user and user.username and user.password
        $http.post(parseUrl + '/users', user, parseHttpConfig).then (result) ->
          newUser = angular.copy(user)
          delete newUser.password
          newUser.objectId = result.data.objectId
          newUser.sessionToken = result.data.sessionToken
          newUser
      else
        $q.reject data:
          error: 'user MUST have fields username & password !'

    login = (username, password) ->
      $http.get(parseUrl + '/login?username=' + encodeURIComponent(username) + '&password=' + encodeURIComponent(password),
        parseHttpConfig).then (result) ->
          result.data

    # https://parse.com/docs/rest#users-linking

    loginOAuth = (authData) ->
      $http.post(parseUrl + '/users', {authData: authData}, parseHttpConfig).then (result) ->
        result.data

    passwordRecover = (email) ->
      $http.post(parseUrl + '/requestPasswordReset', {email: email}, parseHttpConfig).then ->
# return nothing


    toGeoPoint = (lat, lon) ->
      {
      __type: 'GeoPoint'
      latitude: lat
      longitude: lon
      }

    toPointer = (className, sourceObject) ->
      {
      __type: 'Pointer'
      className: className
      objectId: if typeof sourceObject == 'string' then sourceObject else sourceObject[parseObjectKey]
      }

    toDate = (date) ->
      d = Utils.toDate(date)
      if d
        return d.toISOString()
      throw 'Function toDate must be used with a timestamp or a Date object'
但我只得到了一个错误:

Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to:
Error: [$injector:pget] Provider 'ParseUtils' must define $get factory method.
但是我声明$get方法

编辑:

这是一份带有代码的回购协议

这是js编译的coffee文件中的:

angular.module('app').provider('ParseUtils', function() {
  var credentials;
  credentials = {
    applicationId: null,
    restApiKey: null
  };
  this.initialize = function(applicationId, restApiKey) {
    credentials.applicationId = applicationId;
    return credentials.restApiKey = restApiKey;
  };
  return this.$get = function($http, $q, CrudRestUtils, Utils) {
    var createCrud, createUserCrud, getParseData, login, loginOAuth, parseHttpConfig, parseObjectKey, parseUrl, passwordRecover, service, signup, toDate, toGeoPoint, toPointer;
    service = {
      createCrud: createCrud,
      createUserCrud: createUserCrud,
      signup: signup,
      login: login,
      loginOAuth: loginOAuth,
      passwordRecover: passwordRecover,
      toGeoPoint: toGeoPoint,
      toPointer: toPointer,
      toDate: toDate
    };
    parseUrl = 'https://api.parse.com/1';
    parseObjectKey = 'objectId';
    getParseData = function(result) {
      if (result && result.data) {
        if (!result.data[parseObjectKey] && result.data.results) {
          return result.data.results;
        } else {
          return result.data;
        }
      }
    };
    parseHttpConfig = {
      headers: {
        'X-Parse-Application-Id': credentials.applicationId,
        'X-Parse-REST-API-Key': credentials.restApiKey
      }
    };
    createCrud = function(objectClass, _processBreforeSave, _useCache) {
      var endpointUrl;
      endpointUrl = parseUrl + '/classes/' + objectClass;
      service = CrudRestUtils.createCrud(endpointUrl, parseObjectKey, getParseData, _processBreforeSave, _useCache, parseHttpConfig);
      service.savePartial = function(objectToSave, dataToUpdate) {
        var objectId, toUpdate;
        objectId = typeof objectToSave === 'string' ? objectToSave : objectToSave[parseObjectKey];
        toUpdate = angular.copy(dataToUpdate);
        toUpdate[parseObjectKey] = objectId;
        return service.save(toUpdate);
      };
      return service;
    };
    createUserCrud = function(sessionToken, _processBreforeSave, _useCache) {
      var _processBreforeSaveReal, endpointUrl, parseUserHttpConfig;
      endpointUrl = parseUrl + '/users';
      parseUserHttpConfig = angular.copy(parseHttpConfig);
      parseUserHttpConfig.headers['X-Parse-Session-Token'] = sessionToken;
      _processBreforeSaveReal = function(user) {
        delete user.emailVerified;
        if (_processBreforeSave) {
          return _processBreforeSave(user);
        }
      };
      service = CrudRestUtils.createCrud(endpointUrl, parseObjectKey, getParseData, _processBreforeSaveReal, _useCache, parseUserHttpConfig);
      service.savePartial = function(objectToSave, dataToUpdate) {
        var objectId, toUpdate;
        objectId = typeof objectToSave === 'string' ? objectToSave : objectToSave[parseObjectKey];
        toUpdate = angular.copy(dataToUpdate);
        toUpdate[parseObjectKey] = objectId;
        return service.save(toUpdate);
      };
      return service;
    };
    signup = function(user) {
      if (user && user.username && user.password) {
        return $http.post(parseUrl + '/users', user, parseHttpConfig).then(function(result) {
          var newUser;
          newUser = angular.copy(user);
          delete newUser.password;
          newUser.objectId = result.data.objectId;
          newUser.sessionToken = result.data.sessionToken;
          return newUser;
        });
      } else {
        return $q.reject({
          data: {
            error: 'user MUST have fields username & password !'
          }
        });
      }
    };
    login = function(username, password) {
      return $http.get(parseUrl + '/login?username=' + encodeURIComponent(username) + '&password=' + encodeURIComponent(password), parseHttpConfig).then(function(result) {
        return result.data;
      });
    };
    loginOAuth = function(authData) {
      return $http.post(parseUrl + '/users', {
        authData: authData
      }, parseHttpConfig).then(function(result) {
        return result.data;
      });
    };
    passwordRecover = function(email) {
      return $http.post(parseUrl + '/requestPasswordReset', {
        email: email
      }, parseHttpConfig).then(function() {});
    };
    toGeoPoint = function(lat, lon) {
      return {
        __type: 'GeoPoint',
        latitude: lat,
        longitude: lon
      };
    };
    toPointer = function(className, sourceObject) {
      return {
        __type: 'Pointer',
        className: className,
        objectId: typeof sourceObject === 'string' ? sourceObject : sourceObject[parseObjectKey]
      };
    };
    return toDate = function(date) {
      var d;
      d = Utils.toDate(date);
      if (d) {
        return d.toISOString();
      }
      throw 'Function toDate must be used with a timestamp or a Date object';
    };
  };
});

Coffeescript解析器使用函数的最后一句作为函数的返回,this.$get定义是函数的最后一个块,因此它被返回,因此它不是Provider函数的一部分

您可以通过在功能块末尾添加
return
关键字来解决此问题:

this.$get = (...) ->
#inside the $get definition...
    toDate = (date) ->
      d = Utils.toDate(date)
      if d
        return d.toISOString()
      throw 'Function toDate must be used with a timestamp or a Date object'
    #still inside the $get function block
    return
#we're outside the $get function block, same indentation level...
return

现在,您的代码将返回一个函数,错误将消失

hmm这对我不起作用。。。由于编译等原因,我不能坐立不安,但我发布了一份带有源代码的repo。在coffeescript网站上,有一个名为“try coffeescript”的部分,它是一个带有实时JavaScript代码转换的控制台。尝试粘贴您的代码并查看结果。然后您可以使用生成的JavaScript更新您的答案吗?您可以看到现在发生了什么吗?您的函数正在返回get定义。在末尾添加return,如我的答案中所示,然后再次生成它。此外,我可以看到使用toDate定义的get函数中发生了相同的事情,您可能还需要在$get块的末尾添加return语句