没有与提供的标识符相对应的用户记录

时间:2017-05-20 10:48:40

标签: node.js firebase google-cloud-functions

此功能向x天前安装应用程序的用户发送通知,检索其帐户创建日期,并将其fcm令牌保存在数据库中。由于该功能尚未完全正常运行,因此我此时不使用用户令牌是正常的。我有这个错误:

public final class NHRootClass: Mappable, NSCoding {

  // MARK: Declaration for string constants to be used to decode and also serialize.
private struct SerializationKeys {
static let countTotal = "count_total"
static let status = "status"
static let posts = "posts"
static let pages = "pages"
static let count = "count"
}

  // MARK: Properties
public var countTotal: Int?
public var status: String?
public var posts: [NHPosts]?
public var pages: Int?
public var count: Int?

// MARK: ObjectMapper Initializers
/// Map a JSON object to this class using ObjectMapper.
///
/// - parameter map: A mapping from ObjectMapper.
public required init?(map: Map){

}

/// Map a JSON object to this class using ObjectMapper.
///
/// - parameter map: A mapping from ObjectMapper.
public func mapping(map: Map) {
  countTotal <- map[SerializationKeys.countTotal]
  status <- map[SerializationKeys.status]
  posts <- map[SerializationKeys.posts]
  pages <- map[SerializationKeys.pages]
  count <- map[SerializationKeys.count]
}

/// Generates description of the object in the form of a NSDictionary.
///
/// - returns: A Key value pair containing all valid values in the object.
public func dictionaryRepresentation() -> [String: Any] {
  var dictionary: [String: Any] = [:]
  if let value = countTotal { dictionary[SerializationKeys.countTotal] = value }
  if let value = status { dictionary[SerializationKeys.status] = value }
  if let value = posts { dictionary[SerializationKeys.posts] = value.map { $0.dictionaryRepresentation() } }
  if let value = pages { dictionary[SerializationKeys.pages] = value }
  if let value = count { dictionary[SerializationKeys.count] = value }
  return dictionary
}

// MARK: NSCoding Protocol
required public init(coder aDecoder: NSCoder) {
  self.countTotal = aDecoder.decodeObject(forKey: SerializationKeys.countTotal) as? Int
  self.status = aDecoder.decodeObject(forKey: SerializationKeys.status) as? String
  self.posts = aDecoder.decodeObject(forKey: SerializationKeys.posts) as? [NHPosts]
  self.pages = aDecoder.decodeObject(forKey: SerializationKeys.pages) as? Int
  self.count = aDecoder.decodeObject(forKey: SerializationKeys.count) as? Int
}

public func encode(with aCoder: NSCoder) {
aCoder.encode(countTotal, forKey: SerializationKeys.countTotal)
aCoder.encode(status, forKey: SerializationKeys.status)
aCoder.encode(posts, forKey: SerializationKeys.posts)
aCoder.encode(pages, forKey: SerializationKeys.pages)
aCoder.encode(count, forKey: SerializationKeys.count)
}

}

我无法找到它的来源。正如你所看到的,我尝试了很多东西来捕捉错误,但没有任何工作......

Error: There is no user record corresponding to the provided identifier.
    at FirebaseAuthError.Error (native)
    at FirebaseAuthError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:25:28)
    at new FirebaseAuthError (/user_code/node_modules/firebase-admin/lib/utils/error.js:90:23)
    at /user_code/node_modules/firebase-admin/lib/auth/auth-api-request.js:113:15
    at /user_code/node_modules/firebase-admin/lib/auth/auth-api-request.js:332:13
    at process._tickDomainCallback (internal/process/next_tick.js:129:7)

我的数据库:

 'use strict';

    const functions = require('firebase-functions');
    const admin = require('firebase-admin');
    admin.initializeApp(functions.config().firebase);

    exports.sendNotificationToNewUser = functions.https.onRequest((request, response) => {

        Date.prototype.sameDay = function (d) {
            return this.getFullYear() === d.getFullYear()
            && this.getDate() === d.getDate()
            && this.getMonth() === d.getMonth();
        }

        // Loop through users in order with the forEach() method. The callback
        // provided to forEach() will be called synchronously with a DataSnapshot
        // for each child:
        var query = admin.database().ref("users").orderByKey();
        var defaultAuth = admin.auth();
        const getUsersPromise = query.once("value")

        return Promise.all([getUsersPromise]).then(results => {

            const tokensSnapshot = "test";

            results[0].forEach(function (childSnapshot) {
                if (childSnapshot != null) {
                    try {
                        // user will be "ada" the first time and "alan" the second time
                        var user_id = childSnapshot.key;
                        // childData will be the actual contents of the child
                        var user_data = childSnapshot.val();


                        admin.auth().getUser(user_id)
                        .then(function (userRecord) {
                            if (userRecord != null) {
                                var create_date = userRecord.metadata.createdAt
                                var date_now = new Date(Date.now());
                                console.log("Creation date:", create_date);
                                create_date.setDate(create_date.getDate() + 4);
                                if (create_date.sameDay(date_now)) {
                                    //todo something here
                                }
                            }
                        })
                        .catch(function(error) {
                            // Handle Errors here.
                            var errorCode = error.code;
                            var errorMessage = error.message;
                            console.log('User did not sign up correctly');
                            console.log(errorCode);
                            console.console.log(errorMessage);
                        });
                    }
                    catch (e) {
                        console.log(e);
                    }
                }
            });
            // Notification details.
            const payload = {
                notification: {
                    body: "blabla",
                    sound: "default"
                },
                "data": {
                    "key": "CODE",
                    "value": "playstore"
                }
            };

            var options = {
                priority: "high",
                collapseKey: "playstore"
            };

            // Listing all tokens, error here below.
            //const tokens = Object.keys(tokensSnapshot.val());
            const tokens = "my token for the test";

            // Send notifications to all tokens.
            // Send a message to the device corresponding to the provided
            // registration token with the provided options.
            return admin.messaging().sendToDevice(tokens, payload, options).then(response => {
                // For each message check if there was an error.
                const tokensToRemove = [];
                response.results.forEach((result, index) => {
                    const error = result.error;
                    if (error) {
                        console.error('Failure sending notification to', tokens[index], error);
                        // Cleanup the tokens who are not registered anymore.
                        if (error.code === 'messaging/invalid-registration-token' ||
                        error.code === 'messaging/registration-token-not-registered') {
                            tokensToRemove.push(tokensSnapshot.ref.child(tokens[index]).remove());
                        }
                    }
                    else {
                        console.log("Successfully sent message:", response);
                    }
                });
                return Promise.all(tokensToRemove);
            })
        })
    })

0 个答案:

没有答案