Javascript TypeError[ERR_INVALID_ARG_TYPE]:该;路径“;参数的类型必须为字符串。接收到对象的实例
我正在使用from a security rules尝试使用Jest对我的Javascript异步函数Javascript TypeError[ERR_INVALID_ARG_TYPE]:该;路径“;参数的类型必须为字符串。接收到对象的实例,javascript,node.js,firebase,google-cloud-firestore,jestjs,Javascript,Node.js,Firebase,Google Cloud Firestore,Jestjs,我正在使用from a security rules尝试使用Jest对我的Javascript异步函数async_create_post(用于firebase HTTP函数create_post进行集成测试。涉及的文件具有以下目录结构: 测试文件:root/tests/handlers/posts.test.js 要测试的文件:root/functions/handlers/posts.js 教程中的帮助程序代码:root/tests/rules/helpers.js 下面是涉及的源代码: po
async_create_post
(用于firebase HTTP函数create_post
进行集成测试。涉及的文件具有以下目录结构:
测试文件:root/tests/handlers/posts.test.js
要测试的文件:root/functions/handlers/posts.js
教程中的帮助程序代码:root/tests/rules/helpers.js
下面是涉及的源代码:
posts.test.js
const { setup, teardown} = require("../rules/helpers");
const {
async_get_all_undeleted_posts,
async_get_post,
async_delete_post,
async_create_post
} = require("../../functions/handlers/posts");
describe("Post Creation", () => {
afterEach(async () => {
await teardown();
});
test("should create a post", async () => {
const db = await setup();
const malloryUID = "non-existent uid";
const firstPost = {
body: "First post from Mallory",
author_id: malloryUID,
images: ["url1", "url2"]
}
const before_post_snapshot = await db.collection("posts").get();
expect(before_post_snapshot.docs.length).toBe(0);
await async_create_post(firstPost); //fails at this point, expected to create a new post, but instead threw an error
const after_post_snapshot = await db.collection("posts").get();
expect(after_post_snapshot.docs.length).toBe(1);
});
});
const {admin, db } = require('../util/admin');
//admin.initializeApp(config); //my credentials
//const db = admin.firestore();
const { uuid } = require("uuidv4");
const {
success_response,
error_response
} = require("../util/validators");
exports.async_create_post = async (data, context) => {
try {
const images = [];
data.images.forEach((url) => {
images.push({
uid: uuid(),
url: url
});
})
const postRecord = {
body: data.body,
images: images,
last_updated: admin.firestore.FieldValue.serverTimestamp(),
like_count: 0,
comment_count: 0,
deleted: false,
author_id: data.author_id
};
const generatedToken = uuid();
await db
.collection("posts")
.doc(generatedToken)
.set(postRecord);
// return success_response();
return success_response(generatedToken);
} catch (error) {
console.log("Error in creation of post", error);
return error_response(error);
}
}
const functions = require('firebase-functions');
const {
async_get_all_undeleted_posts,
async_get_post,
async_delete_post,
async_create_post
} = require('./handlers/posts');
exports.create_post = functions.https.onCall(async_create_post);
posts.js
const { setup, teardown} = require("../rules/helpers");
const {
async_get_all_undeleted_posts,
async_get_post,
async_delete_post,
async_create_post
} = require("../../functions/handlers/posts");
describe("Post Creation", () => {
afterEach(async () => {
await teardown();
});
test("should create a post", async () => {
const db = await setup();
const malloryUID = "non-existent uid";
const firstPost = {
body: "First post from Mallory",
author_id: malloryUID,
images: ["url1", "url2"]
}
const before_post_snapshot = await db.collection("posts").get();
expect(before_post_snapshot.docs.length).toBe(0);
await async_create_post(firstPost); //fails at this point, expected to create a new post, but instead threw an error
const after_post_snapshot = await db.collection("posts").get();
expect(after_post_snapshot.docs.length).toBe(1);
});
});
const {admin, db } = require('../util/admin');
//admin.initializeApp(config); //my credentials
//const db = admin.firestore();
const { uuid } = require("uuidv4");
const {
success_response,
error_response
} = require("../util/validators");
exports.async_create_post = async (data, context) => {
try {
const images = [];
data.images.forEach((url) => {
images.push({
uid: uuid(),
url: url
});
})
const postRecord = {
body: data.body,
images: images,
last_updated: admin.firestore.FieldValue.serverTimestamp(),
like_count: 0,
comment_count: 0,
deleted: false,
author_id: data.author_id
};
const generatedToken = uuid();
await db
.collection("posts")
.doc(generatedToken)
.set(postRecord);
// return success_response();
return success_response(generatedToken);
} catch (error) {
console.log("Error in creation of post", error);
return error_response(error);
}
}
const functions = require('firebase-functions');
const {
async_get_all_undeleted_posts,
async_get_post,
async_delete_post,
async_create_post
} = require('./handlers/posts');
exports.create_post = functions.https.onCall(async_create_post);
当我在Webstorm IDE中运行测试时,1个终端运行Firebase模拟器:start
,我得到以下错误消息
console.log
Error in creation of post TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received an instance of Object
at validateString (internal/validators.js:120:11)
at Object.basename (path.js:1156:5)
at GrpcClient.loadProto (/Users/isaac/Desktop/project/functions/node_modules/google-gax/src/grpc.ts:166:23)
at new FirestoreClient (/Users/isaac/Desktop/project/functions/node_modules/@google-cloud/firestore/build/src/v1/firestore_client.js:118:38)
at ClientPool.clientFactory (/Users/isaac/Desktop/project/functions/node_modules/@google-cloud/firestore/build/src/index.js:330:26)
at ClientPool.acquire (/Users/isaac/Desktop/project/functions/node_modules/@google-cloud/firestore/build/src/pool.js:87:35)
at ClientPool.run (/Users/isaac/Desktop/project/functions/node_modules/@google-cloud/firestore/build/src/pool.js:164:29)
at Firestore.request (/Users/isaac/Desktop/project/functions/node_modules/@google-cloud/firestore/build/src/index.js:961:33)
at WriteBatch.commit_ (/Users/isaac/Desktop/project/functions/node_modules/@google-cloud/firestore/build/src/write-batch.js:485:48)
at exports.async_create_post (/Users/isaac/Desktop/project/functions/handlers/posts.js:36:5) {
code: 'ERR_INVALID_ARG_TYPE'
}
at exports.async_create_post (/Users/isaac/Desktop/project/functions/handlers/posts.js:44:13)
Error: expect(received).toBe(expected) // Object.is equality
Expected: 1
Received: 0
<Click to see difference>
at Object.<anonymous> (/Users/isaac/Desktop/project/tests/handlers/posts.test.js:59:45)
index.js
const { setup, teardown} = require("../rules/helpers");
const {
async_get_all_undeleted_posts,
async_get_post,
async_delete_post,
async_create_post
} = require("../../functions/handlers/posts");
describe("Post Creation", () => {
afterEach(async () => {
await teardown();
});
test("should create a post", async () => {
const db = await setup();
const malloryUID = "non-existent uid";
const firstPost = {
body: "First post from Mallory",
author_id: malloryUID,
images: ["url1", "url2"]
}
const before_post_snapshot = await db.collection("posts").get();
expect(before_post_snapshot.docs.length).toBe(0);
await async_create_post(firstPost); //fails at this point, expected to create a new post, but instead threw an error
const after_post_snapshot = await db.collection("posts").get();
expect(after_post_snapshot.docs.length).toBe(1);
});
});
const {admin, db } = require('../util/admin');
//admin.initializeApp(config); //my credentials
//const db = admin.firestore();
const { uuid } = require("uuidv4");
const {
success_response,
error_response
} = require("../util/validators");
exports.async_create_post = async (data, context) => {
try {
const images = [];
data.images.forEach((url) => {
images.push({
uid: uuid(),
url: url
});
})
const postRecord = {
body: data.body,
images: images,
last_updated: admin.firestore.FieldValue.serverTimestamp(),
like_count: 0,
comment_count: 0,
deleted: false,
author_id: data.author_id
};
const generatedToken = uuid();
await db
.collection("posts")
.doc(generatedToken)
.set(postRecord);
// return success_response();
return success_response(generatedToken);
} catch (error) {
console.log("Error in creation of post", error);
return error_response(error);
}
}
const functions = require('firebase-functions');
const {
async_get_all_undeleted_posts,
async_get_post,
async_delete_post,
async_create_post
} = require('./handlers/posts');
exports.create_post = functions.https.onCall(async_create_post);
错误消息意味着
path
模块的方法(如path.join
)希望它的一个参数是字符串,但得到了其他参数
我通过二进制搜索找到了有问题的行,并对程序进行了注释,直到错误消失
可能您的某个模块使用了
path
,而您提供了错误的参数。错误消息意味着path
模块的方法(如path.join
)期望其参数之一为字符串,但得到了其他参数
我通过二进制搜索找到了有问题的行,并对程序进行了注释,直到错误消失
可能您的某个模块使用了
path
,而您提供了错误的参数。这里有很多代码,很难判断出哪里出了问题。你能不能把它编辑下来,以显示重现问题的最小代码?大量代码是从在线教程中复制过来的,但我采用了一种文件结构,这种结构是由不同来源的代码组合而成的。我不确定错误消息是否来自我的个人实现或某些安装问题。更新了文章,提供了更多解释。缩小导致问题的特定代码行是调试过程的一部分,这是获得有关堆栈溢出的良好帮助所必需的。如果你发布了数百行代码,那么有人会为你整理所有代码的可能性会大大降低。如果你有一个try-catch块,只需从下到上注释,直到它无法控制台。记录你的错误。但是通读您的代码,我认为data.images可能是object[],但被视为string[]async\u create\u post
的底层实现是数据包含一个images
字符串数组,对于每个字符串,我们希望将其转换为一个映射,带有uid
,以及初始字符串url
。我不知道为什么编译器应该关心我是传入对象数组还是字符串数组。毕竟,不管初始输入images
array中有什么,它都应该创建一个新的数组和一个post记录,对吗?这里有很多代码,很难判断出哪里出了问题。你能不能把它编辑下来,以显示重现问题的最小代码?大量代码是从在线教程中复制过来的,但我采用了一种文件结构,这种结构是由不同来源的代码组合而成的。我不确定错误消息是否来自我的个人实现或某些安装问题。更新了文章,提供了更多解释。缩小导致问题的特定代码行是调试过程的一部分,这是获得有关堆栈溢出的良好帮助所必需的。如果你发布了数百行代码,那么有人会为你整理所有代码的可能性会大大降低。如果你有一个try-catch块,只需从下到上注释,直到它无法控制台。记录你的错误。但是通读您的代码,我认为data.images可能是object[],但被视为string[]async\u create\u post
的底层实现是数据包含一个images
字符串数组,对于每个字符串,我们希望将其转换为一个映射,带有uid
,以及初始字符串url
。我不知道为什么编译器应该关心我是传入对象数组还是字符串数组。毕竟,不管初始输入images
array中有什么,它都应该创建一个新的数组和一个post记录,对吗?