我有以下 2 个文件root/functions/util/constants.js
和root/functions/handlers/images.js
。以下是它们的源代码
图片.js
const path = require("path");
const os = require("os");
const sharp = require('sharp');
const fs = require('fs-extra');
const { uuid } = require("uuidv4");
const { Storage } = require('@google-cloud/storage');
const config = require("../util/config");
const gcs = new Storage({
projectId: config.projectId
});
const bucket = gcs.bucket(config.storageBucket);
const {
INVALID_TYPE_MESSAGE,
POST_SMALL_IMAGE_TYPE,
POST_MEDIUM_IMAGE_TYPE,
select_size_from_type
} = require("../util/constants");
const {
USER_PUBLIC_PROFILE_IMAGE
} = require("../util/schema")
const {error_response} = require("../util/validators");
exports.async_resize = async function(url, type) {
const size = select_size_from_type(type);//code beyond this are not executed
//some other code...
常量.js
const {error_response} = require("./validators");
const { admin } = require("./admin")
//Entity and collection names
//also contains fields of maps of schemas
const {
USER_PUBLIC_PROFILE_IMAGE
} = require("./schema");
const LIKES_COLLECTION = "likes";
const COMMENTS_COLLECTION = "comments";
const POSTS_COLLECTION = "posts";
const NOTIFICATIONS_COLLECTION = "notifications";
const FOLLOWERS = "followers";
const FOLLOWING = "following";
const PUBLIC_FOLLOWERS_SUBCOLLECTION = FOLLOWERS;
const PUBLIC_FOLLOWING_SUBCOLLECTION = FOLLOWING;
const USERS_PUBLIC_COLLECTION = "users_public";
const USERS_PRIVATE_COLLECTION = "users_private";
const PRIVATE_FOLLOWERS_SUBCOLLECTION = FOLLOWERS;
const PRIVATE_FOLLOWING_SUBCOLLECTION = FOLLOWING;
const LIKE_TYPE = "like";
const COMMENT_TYPE = "comment";
const POST_TYPE = "post";
const NOTIFICATION_TYPE = "notification"
const POST_SMALL_IMAGE_TYPE = "small";
const POST_MEDIUM_IMAGE_TYPE = "medium";
const POST_MAXIMUM_ORIGINAL_TYPE = "original";
const SERVER_TIME = admin.firestore.FieldValue.serverTimestamp();
const INVALID_TYPE_MESSAGE = "Invalid recipient type";
const select_collection_from_type = (type) => {
if (type === LIKE_TYPE) {
return LIKES_COLLECTION;
} else if (type === POST_TYPE) {
return POSTS_COLLECTION;
} else if (type === COMMENT_TYPE) {
return COMMENTS_COLLECTION;
} else {
return error_response("Data type is not within scope of project");
}
}
//can be used for profile_image also
const select_size_from_type = function (type) {
if (type === POST_SMALL_IMAGE_TYPE) {
return 64;
} else if (type === POST_MEDIUM_IMAGE_TYPE) {
return 128;
} else if (type === POST_MAXIMUM_ORIGINAL_TYPE) {
return 256;
} else if (type === USER_PUBLIC_PROFILE_IMAGE) {
return 1; //same as before, need a better number for this
//
} else {
return error_response("Data type is not within scope of project");
}
}
module.exports = {
LIKE_TYPE,
LIKES_COLLECTION,
COMMENT_TYPE,
COMMENTS_COLLECTION,
PUBLIC_FOLLOWERS_SUBCOLLECTION,
PUBLIC_FOLLOWING_SUBCOLLECTION,
PRIVATE_FOLLOWERS_SUBCOLLECTION,
PRIVATE_FOLLOWING_SUBCOLLECTION,
USERS_PUBLIC_COLLECTION,
USERS_PRIVATE_COLLECTION,
POST_TYPE,
POSTS_COLLECTION,
INVALID_TYPE_MESSAGE,
SERVER_TIME,
NOTIFICATIONS_COLLECTION,
POST_SMALL_IMAGE_TYPE,
POST_MEDIUM_IMAGE_TYPE,
POST_MAXIMUM_ORIGINAL_TYPE,
select_collection_from_type,
select_size_from_type
};
我尝试运行脚本,它一直告诉我
TypeError: select_size_from_type is not a function
at exports.async_resize (/Users/Isaac/root/functions/handlers/images.js:36:16)
但我真的没有看到任何语法错误。我正在使用网络风暴,可以导航功能。有人可以启发我语法错误的其他原因是什么吗?
更新
我也在images.js
年尝试了console.log(select_size_from_type);
,console.log(select_size_from_type);
在constants.js
年出口之前。它分别打印undefined
和[Function: select_size_from_type]
。即使我尝试更改async_resize
和select_size_from_type
的签名,它们也都给出了相同的错误。任何其他不涉及select_size_from_type
的功能都可以完美运行。
函数可以通过两种方式定义
方式 1
function my_function_name (arguments) {
...function body
}
方式 2
const my_function_name = function (arguments) {
... function body
}
在您的情况下,您似乎正在尝试混合这两种方式。
尝试将函数定义更改为
exports.async_resize = async(url, type) => {
...function body
}
试试这个
const size = this.select_size_from_type(type);//code beyond this are not executed
或像这样导出您的文件
module.exports.select_size_from_type=select_size_from_type
或
module.exports={size_type : select_size_from_type}
然后像这样要求它
const getsizefromtype=require(....)
select_size_from_type= getsizefromtype.size_type