英文:
Deploying Firebase Typescript Function Error; unmarshalling package.json: json: cannot unmarshal bool into Go struct field PackageJSON.devDependencies
问题
以下是您提供的代码的翻译:
我试图部署一个云函数到Firebase,但我得到以下错误消息。
构建失败:解封包 package.json 时出错:json:无法将布尔值解封为 Go 结构字段 PackageJSON.devDependencies 类型的字符串;错误 ID:9f520bcf
这是我的 index.ts 文件:
import * as functions from 'firebase-functions';
import retrievePositionData from './retrievePositionData';
const admin = require('firebase-admin');
admin.initializeApp();
export const fetchLocations = functions.pubsub
.schedule('*/10 * * * *')
.timeZone('America/Montreal')
.onRun(retrievePositionData);
这是我的自定义函数,用于获取 JSON 并写入 Firestore 的 retrievePositionData.ts 文件:
import axios from "axios";
import { firestore } from 'firebase-admin';
import { Client, ElevationResponse } from "@googlemaps/google-maps-services-js";
import { config } from 'firebase-functions';
type SpotResponse = {
response: {
feedMessageResponse: {
count: number,
messages: {
message: Array<{
latitude: number,
longitude: number,
unixTime: number,
messageType: string,
messageContent: string,
batteryState: string,
}>,
},
},
errors: Array<{}>,
},
}
export default async () => {
const client = new Client({});
const FirestoreInstance = firestore();
const feedID = '0u88e3hrgRoCDA4JEqkg41U1jhO25PbvW';
let offset = 0;
while (true) {
const url = `https://api.findmespot.com/spot-main-web/consumer/rest-api/2.0/public/feed/${feedID}/message.json?start=${offset}`;
const response = await axios.get<SpotResponse>(url)
if (response.data.response.errors) {
console.log('已达到反馈的末尾。停止。');
return;
}
const messages = response.data.response.feedMessageResponse.messages.message;
while (messages.length > 0) {
const message = messages.shift();
if (!message) throw new Error('队列中没有更多项目');
const query = await FirestoreInstance.collection('locationHistory').where('timestamp', '==', firestore.Timestamp.fromMillis(message.unixTime * 1000)).limit(1).get();
if(!query.empty) {
console.log('找到重复条目。停止。');
return;
}
const elevationResponse: ElevationResponse = await client.elevation({
params: {
locations: [{ lat: message.latitude, lng: message.longitude }],
key: config().maps.key,
},
timeout: 1000,
});
const formatedMessage: any = {
timestamp: firestore.Timestamp.fromMillis(message.unixTime * 1000),
location: new firestore.GeoPoint(message.latitude, message.longitude),
messageType: message.messageType,
messageContent: message.messageContent,
batteryState: message.batteryState,
elevation: elevationResponse.data.results[0].elevation,
}
Object.keys(formatedMessage).forEach(key => formatedMessage[key] === undefined && delete formatedMessage[key]);
FirestoreInstance.collection('locationHistory').doc().create(formatedMessage)
.then((value: firestore.WriteResult) => {
console.log('成功插入文档', formatedMessage, '于', value.writeTime);
})
.catch((reason: any) => {
console.error('无法插入文档', reason);
});
}
offset += response.data.response.feedMessageResponse.count;
}
}
这是 package.json 文件:
{
"name": "functions",
"scripts": {
"lint": "tslint --project tsconfig.json",
"build": "tsc",
"serve": "npm run build && firebase emulators:start --only functions",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "10"
},
"main": "lib/index.js",
"dependencies": {
"@googlemaps/google-maps-services-js": "^3.1.4",
"axios": "^0.19.2",
"express": "^4.17.1",
"firebase-admin": "^8.10.0",
"firebase-functions": "^3.6.1"
},
"devDependencies": {
"tslint": "^5.12.0",
"typescript": "^4.9.4",
"firebase-functions-test": "^0.2.0",
"private": true
}
}
希望这对您有所帮助!如果您需要更多翻译,请告诉我。
英文:
I'm trying to deploy a cloud function to firebase and I'm getting the following error message.
Build failed: unmarshalling package.json: json: cannot unmarshal bool into Go struct field PackageJSON.devDependencies of type string; Error ID: 9f520bcf
This is my index.ts
import * as functions from 'firebase-functions';
import retrievePositionData from './retrievePositionData';
const admin = require('firebase-admin');
admin.initializeApp();
export const fetchLocations = functions.pubsub
.schedule('*/10 * * * *')
.timeZone('America/Montreal')
.onRun(retrievePositionData);
This is my custom function pulling JSON to write to Firestore retrievePositionData.ts.
import axios from "axios";
import { firestore } from 'firebase-admin';
import { Client, ElevationResponse } from "@googlemaps/google-maps-services-js";
import { config } from 'firebase-functions';
type SpotResponse = {
response: {
feedMessageResponse: {
count: number,
messages: {
message: Array<{
latitude: number,
longitude: number,
unixTime: number,
messageType: string,
messageContent: string,
batteryState: string,
}>,
},
},
errors: Array<{}>,
},
}
export default async () => {
const client = new Client({});
const FirestoreInstance = firestore();
const feedID = '0u88e3hrgRoCDA4JEqkg41U1jhO25PbvW';
let offset = 0;
while (true) {
const url = `https://api.findmespot.com/spot-main-web/consumer/rest-api/2.0/public/feed/${feedID}/message.json?start=${offset}`;
const response = await axios.get<SpotResponse>(url)
if (response.data.response.errors) {
console.log('Reached the end of the feed. Stopping.');
return;
}
const messages = response.data.response.feedMessageResponse.messages.message;
while (messages.length > 0) {
const message = messages.shift();
if (!message) throw new Error('No more items in the queue');
const query = await FirestoreInstance.collection('locationHistory').where('timestamp', '==', firestore.Timestamp.fromMillis(message.unixTime * 1000)).limit(1).get();
if(!query.empty) {
console.log('Found duplicate entry. Stopping.');
return;
}
const elevationResponse: ElevationResponse = await client.elevation({
params: {
locations: [{ lat: message.latitude, lng: message.longitude }],
key: config().maps.key,
},
timeout: 1000,
});
const formatedMessage: any = {
timestamp: firestore.Timestamp.fromMillis(message.unixTime * 1000),
location: new firestore.GeoPoint(message.latitude, message.longitude),
messageType: message.messageType,
messageContent: message.messageContent,
batteryState: message.batteryState,
elevation: elevationResponse.data.results[0].elevation,
}
Object.keys(formatedMessage).forEach(key => formatedMessage[key] === undefined && delete formatedMessage[key]);
FirestoreInstance.collection('locationHistory').doc().create(formatedMessage)
.then((value: firestore.WriteResult) => {
console.log('Successfully inserted document', formatedMessage, 'at', value.writeTime);
})
.catch((reason: any) => {
console.error('Could not insert document', reason);
});
}
offset += response.data.response.feedMessageResponse.count;
}
}
And this is package.json
{
"name": "functions",
"scripts": {
"lint": "tslint --project tsconfig.json",
"build": "tsc",
"serve": "npm run build && firebase emulators:start --only functions",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "10"
},
"main": "lib/index.js",
"dependencies": {
"@googlemaps/google-maps-services-js": "^3.1.4",
"axios": "^0.19.2",
"express": "^4.17.1",
"firebase-admin": "^8.10.0",
"firebase-functions": "^3.6.1"
},
"devDependencies": {
"tslint": "^5.12.0",
"typescript": "^4.9.4",
"firebase-functions-test": "^0.2.0",
"private": true
}
}
答案1
得分: 0
Remove this line from package.json. It's not a devDependency, yet you have it nested there:
"private": true
英文:
Remove this line from package.json. It's not a devDependency, yet you have it nested there:
"private": true
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论